HELP with relection on Interfaces

C

Cairn

I have some .net code I am using which I also which to use on a compact
device.
The code looks in the given dir and then loads all instances of the given
interface. I have attempted to convert the code to complei and run on a
device to to no luck. please does anyone have to ideas on what to change.


Dictionary<short, IPlugin> pluginlist;

void load (string filename)
{
Assembly plugin_assembly = null;

//Load the assembly to memory so we don't lock up the file
byte[] assemblyFileData = System.IO.File.ReadAllBytes(filename);

//Load the assembly
try
{
plugin_assembly = Assembly.Load(assemblyFileData);
}
catch
{
errorMsg = "Compiled Assembly (" + filename + ") is not a valid
Assembly File to be Loaded.";
}

if (plugin_assembly != null)
{
string typeName = typeof(IPlugin).ToString();
//Go through the types we need to find our clientinterface
foreach (Type t in plugin_assembly.GetTypes())
{
//Try getting the interface from the current type
if (t.GetInterface(typeName, true) != null)
{
IPlugin ish = (IPlugin)Activator.CreateInstance(t);
luginlist.Add(ish.Plugin_id, ish);
}
}
}
 
C

Chris Tacke, eMVP

Telling us what exactly fails would be useful. Anything else expects us to
put this into a compiler, add code to make it compile and then run it, which
few will be likely to do. I *think* the issue is that GetInterface doesn't
exist in the CF, only GetInterfaces. So call getInterfaces, walk the list
and see if the one you want is there - pretty basic development problem.

Something like this that I use in another project:

public static bool DerivesFrom(this Type baseobject, Type interfaceType)
{
foreach (Type t in baseobject.GetInterfaces())
{
if (t.Equals(interfaceType))
return true;
}

return false;
}



--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top