Click here to Skip to main content
15,888,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Wonder if anyone can advice on best practice using a reflection.
The case I got is as follows:
I got a dll file with multiple classes. Some of those classes implement a particular interface "GetStream". This interface has a property called "Name".
Now - I need to load that dll file and invoke a method of a class implementing "GetStream", where property Name = "SomeName".
Any advice on how to do that without initializing all the classes and checking instances?

I have written the following code, but it fails somewhy:

C#
// load the dll file
Assembly typeLibrary = Assembly.LoadFile(Directory.GetCurrentDirectory() + fileLoc);
string streamName = "SomeName";
foreach (Type t in typeLibrary.GetTypes())
{
   if (t.GetInterfaces().Contains(typeof(GetStream)) && t.Name.Equals(streamName))
   {
      // I never actually get here even, because t.GetInterfaces never contains "GetStream" for some reasons.
      // checking this with a debugger shows exact same type Name and FullName.. I must be missing something here...
   }
}


Any ideas of what that might be, that I am missing ?

By the way - I would prefer avoiding to hardcode the interface name into string like this:
C#
t.GetInterface("GetStream") != null

Event hough it works this does not look like a clean approach and might cause runtime errors if the interface name is changed (there would be no build errors).
Posted
Updated 31-Jan-16 23:55pm
v3
Comments
F-ES Sitecore 1-Feb-16 5:45am    
You can use "as" to cast to the interface and if it doesn't support the interface it'll return null.

GetStream gs = myObject as GetStream; // gs will be null if it doesn't support GetStream
Sascha Lefèvre 1-Feb-16 5:56am    
You don't have a 'myObject' here, just Types.
MK-Gii 1-Feb-16 5:58am    
In this case I would have to create instance for each object in the assembly and then try to cast it. I got like 2000 classes here so creating instance for all classes and casting it would not be efficient.
dan!sh 1-Feb-16 6:01am    
[Sascha's correction invalidPoint]Is name static? [/Sascha's correction] If not, how will you check value without an object?
Sascha Lefèvre 1-Feb-16 6:07am    
Name can't be static, it's part of an interface.

1 solution

If you remove the && t.Name.Equals(streamName) -part your code will enter the if-block provided that there actually is a Type that implements that interface.

But there's a conceptual problem here: You're dealing with Types only here. There are no instances of any Types and so there can't be any values of properties that are part of an interface (because those can't be static).

Theoretically you would have to create instances of all those Types which implement that interface in order to check the value of that property. But it smells more like you need to re-work your idea here. If you elaborate on why you think you need to do this, we might have an idea how it could be done (differently, probably).

Edit: Btw: t.Name isn't the value of the Name-property. It's the name of the Type. If there wasn't the coincidence that Type has a property 'Name' your code wouldn't compile.
 
Share this answer
 
v3
Comments
MK-Gii 1-Feb-16 6:18am    
Very true. Thanks.
Since my property was of name "Name" I was a bit deceived. And that Name simply returned the class name. The only thing I needed to check more was if the interface is implemented or not.
Sascha Lefèvre 1-Feb-16 6:30am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900