Hey there,

I'm quite new to C# but have an extensive background in Java so I know all the basics and perhaps more. But I've found i'm struggling with C#'s answer to dynamic binding at runtime.

Whilst Java can take a namespace entry from an interface and turn it into a class instance it appears that C# can't? Perhaps I'm doing it wrong but I've looked into the System.Reflection namespace and nothing appears to work like Java does.

now in Java the Class is actually a Class of type Class. Meaning you can instaniate a class of this type which is derived from Object. C# doesn't have this but yet you can still query meta data on the class required.

Is there a way to get C# to construct an object of a class from an interface namespace?

Hope this makes sense, if not I can post more information.

9 Years
Discussion Span
Last Post by fuzzelogic

Yes. You can walk through the classes/structs etc of an application and then create instances.

For example

Assembly a = null;
            AssemblyName n = new AssemblyName("mynamespace");
            a = Assembly.Load(n);
            Type[] ts = a.GetTypes();

            foreach (Type t in ts)
                if (t.Name == "MyClass")
                    newcmd = (MyClass)Activator.CreateInstance(t);


Will go through your code and make an instance of "MyClass" theres probably a quicker way but I took that from some of my code I use to walk through and make instances of all things derrived from a specific class.


hmm,so you can't pass a class into a method and cast it?


loadAllClasses(class T) {

//load through all DLL's...

create object of type T

return T

Basically all i need to do is create classes on the fly and add them to a HashMap<class>, or something of a smiliar nature, then use an singleton, factory class to obtain a layer of seperation in my applciation. this will allow for a text file to decide which classes are loaded at runtime.

I'm beginning to think this isn't possible in C#?


Probably but you could probably do it based around the code I gave but I havent needed to go deeper than that


Hi! If you looking for something like java's Create proxy, C# does not have it built in. here's a link to a solution ... http://www.codeproject.com/KB/cs/dynamicproxy.aspx

I'm not sure its proxy that I'm looking for? Perhaps the name deems that opinion. Here's more information on what I'm trying to acheive.


I've got an interface called DAOInterface, which can have 2 implementations. 1 which updates the database, the other which just soley updates member variables - doesn't use the database, this is ideal for demonstrations.

So i was wondering if I could produce a class out of the interface namespace. Say for example if I had a text file that said:



this would provide the path of the interface and the direct path to the class that I need.

Can C# do this? The 'class' is just a keyword, where is Java its an actual object.

doing the following in Java is perfectly legal HashMap<Class, Class> where is C# its not!


But as long as they descend from the same base ansestor you could do it, and using code similar to that, and like what I gave you


The obly bit I'm struggling with is creating an HashMap that takes classes, otherwise how am I going to say

GetClass(x) where X is a key if they are all objects? Somewhere I'd have to cast it to the child object but knowing which object I've got would be ideal


Hi .. Is what u looking for generics.. ie..
Dictionary<TInterfaceType,object> hash = new Dictionary<TInterfaceType,object>();

then in code you use the interface type as the type???

Is that what u looking for !!


How can you the interface type has the type? Its just an interface in C# rather than a class isn't it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.