getting list of classes interfaces

  • Thread starter Saso Zagoranski
  • Start date
S

Saso Zagoranski

Hi!

Here's my problem:
Let's say I have 2 interfaces:
interface Module1
{
public void method1();
}

interface Module2
{
public void method2();
}

I'm using late binding, so at compile-time I don't know which modules I'll
be loading as I want
to provide a simple plug-in functionality.
So now I load my class from assembly.dll. How do I know which interfaces has
my class
implemented? Module1, Module2 or perhaps both or even none.

As mentioned before I would like to provide a simple plugin functionality,
similar to the one VS.NET has.
So if someone writes a plugin for my application he would implement the
IModule interface.
If the users module would like to add something to the menubar, he would
implement
IModuleMenu as well.

Is this a good way of doing this?

thanks,
saso
 
C

Cezary Nolewajka

Here you have a sample code. The idea is to use Reflection to discover type
information at runtime:

public interface IModuleOne
{
string GetInfo ();
}

public interface IModuleTwo
{
string GetData ();
}

public class TestClass : IModuleOne, IModuleTwo
{
public string GetInfo ()
{
return "Info";
}

public string GetData ()
{
return "Data";
}
}


class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
Type[] myObjectArray= typeof(TestClass).GetInterfaces();
Console.WriteLine("The Interfaces inherited by the MyTemplate class are
:\n");
for (int index = 0; index < myObjectArray.Length; index++)
{
Console.WriteLine(myObjectArray[index].FullName + ", " +
myObjectArray[index].Name);
}
}
catch (Exception e)
{
Console.WriteLine("Exception Raised !");
Console.WriteLine("Message : " + e.Message);
}
Console.ReadLine ();
}
}
 
S

Sa¹o Zagoranski

thanks... that help a lot! :)

Cezary Nolewajka said:
Here you have a sample code. The idea is to use Reflection to discover type
information at runtime:

public interface IModuleOne
{
string GetInfo ();
}

public interface IModuleTwo
{
string GetData ();
}

public class TestClass : IModuleOne, IModuleTwo
{
public string GetInfo ()
{
return "Info";
}

public string GetData ()
{
return "Data";
}
}


class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
Type[] myObjectArray= typeof(TestClass).GetInterfaces();
Console.WriteLine("The Interfaces inherited by the MyTemplate class are
:\n");
for (int index = 0; index < myObjectArray.Length; index++)
{
Console.WriteLine(myObjectArray[index].FullName + ", " +
myObjectArray[index].Name);
}
}
catch (Exception e)
{
Console.WriteLine("Exception Raised !");
Console.WriteLine("Message : " + e.Message);
}
Console.ReadLine ();
}
}

--
Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply

Saso Zagoranski said:
Hi!

Here's my problem:
Let's say I have 2 interfaces:
interface Module1
{
public void method1();
}

interface Module2
{
public void method2();
}

I'm using late binding, so at compile-time I don't know which modules I'll
be loading as I want
to provide a simple plug-in functionality.
So now I load my class from assembly.dll. How do I know which interfaces has
my class
implemented? Module1, Module2 or perhaps both or even none.

As mentioned before I would like to provide a simple plugin functionality,
similar to the one VS.NET has.
So if someone writes a plugin for my application he would implement the
IModule interface.
If the users module would like to add something to the menubar, he would
implement
IModuleMenu as well.

Is this a good way of doing this?

thanks,
saso
 

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