Late binding equivalent for the .Net class librarys...

  • Thread starter Thread starter Daniel Bass
  • Start date Start date
D

Daniel Bass

..Net is great for modulerising libraries, so that all you need do to access
a DLL, is simply call Add Reference and wallah, it's as though the library
were written in your project.

But what happens when i know that a library must have some method, say

void StoreXML ( string XMLmsg )

but want to late bind, as in, only at run time, decide which library i wish
to use.

COM allows you to design a standard interface, and then any object
implementing that interface can be called by an application using that
standard. It's great for being able to add "plug-in" type technology, having
DLL's there and ready to use, without going back and recompiling your
original application.

Is this sort of thing possible with the .Net Class Librarys?

I've googled a bit buy can't find anything talking about what I'm trying to
do...

Thanks for your time.
Daniel.
 
You can obtain late binding in .NET by using Reflection. For example, I want
to invoke the TestMe method on an object type.

object o;
string methodName = "TestMe";

System.Reflection.MethodInfo mi = o.GetType().GetMethod("TestMe");
mi.Invoke(o,new object[] {});


--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Thanks for that, but I don't see how this binds to the object sitting in
another DLL...

MSDN library uses this GetMethod in a slightly differing context.

Dan

You can obtain late binding in .NET by using Reflection. For example, I want
to invoke the TestMe method on an object type.

object o;
string methodName = "TestMe";

System.Reflection.MethodInfo mi = o.GetType().GetMethod("TestMe");
mi.Invoke(o,new object[] {});


--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Why not simply derive all your objects from a given interface, maybe
ISaveAsXml?
Daniel Bass said:
Thanks for that, but I don't see how this binds to the object sitting in
another DLL...

MSDN library uses this GetMethod in a slightly differing context.

Dan

You can obtain late binding in .NET by using Reflection. For example, I want
to invoke the TestMe method on an object type.

object o;
string methodName = "TestMe";

System.Reflection.MethodInfo mi = o.GetType().GetMethod("TestMe");
mi.Invoke(o,new object[] {});


--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


Daniel Bass said:
.Net is great for modulerising libraries, so that all you need do to access
a DLL, is simply call Add Reference and wallah, it's as though the library
were written in your project.

But what happens when i know that a library must have some method, say

void StoreXML ( string XMLmsg )

but want to late bind, as in, only at run time, decide which library i wish
to use.

COM allows you to design a standard interface, and then any object
implementing that interface can be called by an application using that
standard. It's great for being able to add "plug-in" type technology, having
DLL's there and ready to use, without going back and recompiling your
original application.

Is this sort of thing possible with the .Net Class Librarys?

I've googled a bit buy can't find anything talking about what I'm trying to
do...

Thanks for your time.
Daniel.
 
I'm doing that already...
I've created a simple "calculator" application to work this out.

given to numbers, and a Calculate method, i've designed an interface as so:
[in OperationInterface.dll]

namespace Operation
{
public interface IOperation
{
int Calculate ( int a, int b );
}
}

now for each operation, I derive from it, for example
[in Addition.dll]

namespace Operation
{
public class Addition : IOperation
{
public Addition()
{
}

public int Calculate ( int a, int b )
{
return a + b;
}
}
}


now in my main application, calcultar i add a reference to the
OperationInterface.dll so I can see the interface.
How do I get an implementation of an object? (say the addition class).

i've just the reflection.methodinfo approach, as well as
Activator.CreateInstance, but i just get null objects back on the references
in question.

I must admit, I'm sure I'm overlooking it and it's simple (as you probably
know, and I'm yet to discover) but surely this should have been more of a
priority ( or pre-requisite) when creating the model for the .Net class
library's? The idea behind late binding should be right up there with the
drag and dropping of tables from sqlServer (and all connections/commands
being setup etc), and the intellisense etc... It's great to link to .net
library's at design time with a simple "Add Reference" call, shame the it
seems to be a let down in the runtime dept, or is that me just being
cynical. ;o)

thanks for your help.
Dan.


Why not simply derive all your objects from a given interface, maybe
ISaveAsXml?
Daniel Bass said:
Thanks for that, but I don't see how this binds to the object sitting in
another DLL...

MSDN library uses this GetMethod in a slightly differing context.

Dan

You can obtain late binding in .NET by using Reflection. For example, I want
to invoke the TestMe method on an object type.

object o;
string methodName = "TestMe";

System.Reflection.MethodInfo mi = o.GetType().GetMethod("TestMe");
mi.Invoke(o,new object[] {});


--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


Daniel Bass said:
.Net is great for modulerising libraries, so that all you need do to access
a DLL, is simply call Add Reference and wallah, it's as though the library
were written in your project.

But what happens when i know that a library must have some method, say

void StoreXML ( string XMLmsg )

but want to late bind, as in, only at run time, decide which library i wish
to use.

COM allows you to design a standard interface, and then any object
implementing that interface can be called by an application using that
standard. It's great for being able to add "plug-in" type technology, having
DLL's there and ready to use, without going back and recompiling your
original application.

Is this sort of thing possible with the .Net Class Librarys?

I've googled a bit buy can't find anything talking about what I'm trying to
do...

Thanks for your time.
Daniel.
 
Daniel Bass said:
I'm doing that already...
I've created a simple "calculator" application to work this out.

given to numbers, and a Calculate method, i've designed an interface as so:
[in OperationInterface.dll]

namespace Operation
{
public interface IOperation
{
int Calculate ( int a, int b );
}
}

now for each operation, I derive from it, for example
[in Addition.dll]

namespace Operation
{
public class Addition : IOperation
{
public Addition()
{
}

public int Calculate ( int a, int b )
{
return a + b;
}
}
}


now in my main application, calcultar i add a reference to the
OperationInterface.dll so I can see the interface.
How do I get an implementation of an object? (say the addition class).

i've just the reflection.methodinfo approach, as well as
Activator.CreateInstance, but i just get null objects back on the references
in question.

Hmm, what code are you using for CreateInstance? Generally
Activator.CreateInstance(TypeName) works(Type name in the form
System.String, mscorlib for example, <type>, <assembly>). You should also be
able to load the given assemblies and enumerate them for types implementing
the given interface:

this is a simple(and untested) example:
using System.Reflection;
using System.Collections;
....
IList objectTypes = new ArrayList();
//fill in your assembly
Assembly asm = Assembly.LoadFile(<assembly>);
Type[] types = asm.GetTypes();
foreach (Type type in types)
{
foreach (Type interfaceType in type.GetInterfaces(0))
{
//fill in your interface type name, if its not ISaveAsXml
if (interfaceType == typeof(ISaveAsXml))
{
objectTypes.Add(interfaceType);
}
}
}
I must admit, I'm sure I'm overlooking it and it's simple (as you probably
know, and I'm yet to discover) but surely this should have been more of a
priority ( or pre-requisite) when creating the model for the .Net class
library's? The idea behind late binding should be right up there with the
drag and dropping of tables from sqlServer (and all connections/commands
being setup etc), and the intellisense etc... It's great to link to .net
library's at design time with a simple "Add Reference" call, shame the it
seems to be a let down in the runtime dept, or is that me just being
cynical. ;o)
Well, reflection is pretty broad, I would suspect you are having specific
issues with it. If you wish to post some sample code, someone will probably
be able to advise you on specifically what.
thanks for your help.
Dan.


Why not simply derive all your objects from a given interface, maybe
ISaveAsXml?
Daniel Bass said:
Thanks for that, but I don't see how this binds to the object sitting in
another DLL...

MSDN library uses this GetMethod in a slightly differing context.

Dan

You can obtain late binding in .NET by using Reflection. For example, I want
to invoke the TestMe method on an object type.

object o;
string methodName = "TestMe";

System.Reflection.MethodInfo mi = o.GetType().GetMethod("TestMe");
mi.Invoke(o,new object[] {});


--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


Daniel Bass said:
.Net is great for modulerising libraries, so that all you need do to access
a DLL, is simply call Add Reference and wallah, it's as though the library
were written in your project.

But what happens when i know that a library must have some method, say

void StoreXML ( string XMLmsg )

but want to late bind, as in, only at run time, decide which library i wish
to use.

COM allows you to design a standard interface, and then any object
implementing that interface can be called by an application using that
standard. It's great for being able to add "plug-in" type technology, having
DLL's there and ready to use, without going back and recompiling your
original application.

Is this sort of thing possible with the .Net Class Librarys?

I've googled a bit buy can't find anything talking about what I'm
trying
to
do...

Thanks for your time.
Daniel.
 
Back
Top