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:
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))
{
}
}
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:
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).