Sign in to follow this  
invicticide

C#: Dynamic data types?

Recommended Posts

I'm just *barely* getting started into C# and I've run into a small issue. I've written a class which is supposed to instantiate and manage various types of objects, all of which inherit from a common, abstract base. The instantiation happens through a function call; so is there is any way I can pass the *type* to instance as a parameter to that function?
// the function definition
public BaseClass MakeObject(TYPE theType) <<-- what should TYPE be?
{
    BaseClass BC = new theType; // <-- can I do this???
    return BC;
}

How would that look? Or is it not supported in C#? That is all.

Share this post


Link to post
Share on other sites
It ain't pretty, but sure:
public BaseClass MakeObject(System.Type theType) {
return (BaseClass)theType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
}
(it's been awhile, so I may have gotten this wrong)

You'd call it like so:
BaseClass foo = MakeObject(typeof(MyConcreteSubclass));

Share this post


Link to post
Share on other sites
Quote:
Original post by the Speed Bump
It ain't pretty, but sure:
public BaseClass MakeObject(System.Type theType) {
return (BaseClass)theType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
}
(it's been awhile, so I may have gotten this wrong)

You'd call it like so:
BaseClass foo = MakeObject(typeof(MyConcreteSubclass));

Yeah, umm...no.

// The assert is optional, as the return cast will throw an exception (InvalidCastException)
// however it is a good practice
public BaseClass MakeObject(System.Type theType) {
System.Diagnostics.Debug.Assert(theType.IsSubClassOf(typeof(BaseClass)));
object o = theType.Assembly.CreateInstance(theType.FullName);
return (BaseClass)o;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
I'm curious as well actually, I have a plugin system written in C# that the first method and all works fine.


Yes, actually, it does work fine. It is also perfectly legal. However, it is not intuitive. When reading that code, whomever goes through it will probably not have a clue as to exactly what is going on without having to look up the ConstructorInfo.Invoke method.

In my way, you can clearly see that an instance is being created and returned. The code is self documenting in other words.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Yes, actually, it does work fine. It is also perfectly legal. However, it is not intuitive. When reading that code, whomever goes through it will probably not have a clue as to exactly what is going on without having to look up the ConstructorInfo.Invoke method.

In my way, you can clearly see that an instance is being created and returned. The code is self documenting in other words.

Ah, that's fair enough. Personally I find both ways fairly intuitive, but I'm not exactly looking at it for the first time either. :)

Share this post


Link to post
Share on other sites
I ended up using Washu's method and everything appears to be good. Thanks much!

Speed Bump, I was rather surprised to see you! Are you still working on that Verge spinoff you started oh-so-long ago? (Sorry, I can't remember what it was called right now... ~shame~)

EDIT: Ika! :D

I used to be a semi-but-not-so-productive member of the Verge community a few years back... ;)

That is all.

[Edited by - invicticide on September 21, 2004 6:44:16 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this