Help with some Reflection issues (syntax etc. ?) ...........

This forum is meant for anything you would like to share with other visitors
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Help with some Reflection issues (syntax etc. ?) ...........

Post by Phil Hepburn »

Hi guys, Team and all,

I could do with some help please - regarding the correct X# syntax to make 'GetType()' work with suitable text string input. I have it working for some input, but not others / all.

Here is a working example - where simply the Class is provided as a Namespace.Class string :-
ForumGT_01.jpg
ForumGT_01.jpg (130.57 KiB) Viewed 269 times
Even the fuller two parameter string input works as well (line 211) :-
ForumGT_02.jpg
ForumGT_02.jpg (143.49 KiB) Viewed 269 times
However, my problems come when I try another assembly altogether, outside that of the common two of 'System.dll' and 'mscorlib.dll' - see below :-
ForumGT_03.jpg
ForumGT_03.jpg (73.44 KiB) Viewed 269 times
Remember, I wish to have a string input to the 'Type.GetType()' method and not be told to reach for the operator 'typeof()' which is working the in the last image shown below :-
ForumGT_04.jpg
ForumGT_04.jpg (28.39 KiB) Viewed 269 times
I am well aware that the input text needs to be case sensitive.

Could it be that 'System.Drawing' is closed in some way - strong naming or whatever ?

HELP would be most appreciated.
TIA & Best regards,
Phil.
Wales, UK.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Help with some Reflection issues (syntax etc. ?) ...........

Post by Phil Hepburn »

Sorry guys,

I seem to have posted the wrong image regarding the second input of 'Drawing.dll', I did do it right in one of my tests supplying 'System.Drawing.dll'.

I got a bit confused when I could not attach the images yesterday when I had all the stuff prepared for a posting. Doing it again 'cold' today is my excuse ;-0)

Help still needed,
Phil.
User avatar
Chris
Posts: 4584
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Help with some Reflection issues (syntax etc. ?) ...........

Post by Chris »

Hi Phil,

First of all, the name of the dll is not Drawing.dll, but it's System.Drawing.dll. Also, with this method (Type.GetType()), for extrnal types (not defined in mscorlib.dll) you need to specify the fully qualified name of the type, which includes full assembly (ll) name information, so you'd need something like:

Type.GetType("System.Drawing.Color, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

Probably too much trouble doing it like that, so I think it's better to get first an Assembly object of the dll that contains the type, then do a simple oAssembly:GetType(), where you can specify the type simply by its name.

Chris
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Help with some Reflection issues (syntax etc. ?) ...........

Post by Phil Hepburn »

Thanks Chris,

I will give it a go - both ways, so I can at least then explain 'stuff' in my eNotes, and publish some code that does work and produce results.

Have you any idea why there is such a change required ? Is it because there is no way they (MS) are going to change the core assemblies, but reserve the right etc. to change assemblies more on the 'fringe' of things ?

Any ideas ?
Cheers,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Help with some Reflection issues (syntax etc. ?) ...........

Post by Phil Hepburn »

Chris Pyrgas wrote:Hi Phil,

Type.GetType("System.Drawing.Color, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

Chris
Chris, I can find the details of most of this in the Properties Pane - BUT - where do I get the 'PublicKeyToken' from ?

Regards,
Phil.
User avatar
Chris
Posts: 4584
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Help with some Reflection issues (syntax etc. ?) ...........

Post by Chris »

Hi Phil,

One easy way is to add a reference to System.Drawing in a project, then type this in Start()r somewhere else:

? System.Drawing.Point{}:GetType():AssemblyQualifiedName

this will give you the complete name of the type that you need, together with the full assembly name.

But I think there's no point at all to go that route, I don't think anybody is ever using Type.GetType() this way. Normal way is to get an Assembly object (either by directly loading from disk with Assembly.LoadFrom() or LoadFile()), or get it through other means, for example Assembly.GetEntryAssembly():GetReferencedAssemblies() gives the referenced assemblies of your main app).

Chris
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Help with some Reflection issues (syntax etc. ?) ...........

Post by Phil Hepburn »

Thanks again Chris,

I realise the general ideas and comments you make, and the advice too, AND, I will reflect this in what I write in the eNotes.

I am going to look deep into GetAssemblies() etc. tomorrow.

It seems like the 'good' idea I had for interactively inputting Class 'string' names and getting back the members list etc., is probably NOT such a good idea as it first seemed ;-0((

Don't worry, I will sort it all out tomorrow, and make it sensible. Fingers crossed.

Best regards,
Phil.
User avatar
Chris
Posts: 4584
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Help with some Reflection issues (syntax etc. ?) ...........

Post by Chris »

Hi Phil,

The problem with the first method you used, is that the type you ask for maybe declared in one of the thousands of .Net dlls that are included in every PC (unless it's one of the standard types, defined in mscorlib.dll). So in order to avoid loading and examining (after finding!) every single dll in the disk in order to find the type you are asking for, Type.GetType() requires to specify and describe completely the dll file where the type is located in, so it can load it at runtime and get the type defined in it. And still AFAIK it will not find types in dlls located just in some folder in the PC, but only if the dll is also registered in the GAC.

For this reason, I think using Type.GetType(string) is completely impractical and you need to use one of the more straightforward methods of loading the assembly manually first and then locate the type inside the assembly.

Chris
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Help with some Reflection issues (syntax etc. ?) ...........

Post by Phil Hepburn »

Thanks Chris,

I agree, and see what its all about.

I will adjust my researching and the way I attack the problem, and the way I write the eNotes. This is not a topic that I have given much attention to over the years - hence I came at it in the wrong way / direction.

Regards &
Have a nice day,
Phil.
Post Reply