// the function definition
public BaseClass MakeObject(TYPE theType) <<-- what should TYPE be?
{
BaseClass BC = new theType; // <-- can I do this???
return BC;
}
C#: Dynamic data types?
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?
How would that look? Or is it not supported in C#?
That is all.
It ain't pretty, but sure:
You'd call it like so:
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));
Quote:Original post by the Speed Bump
It ain't pretty, but sure:(it's been awhile, so I may have gotten this wrong)public BaseClass MakeObject(System.Type theType) { return (BaseClass)theType.GetConstructor(System.Type.EmptyTypes).Invoke(null);}
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 practicepublic BaseClass MakeObject(System.Type theType) { System.Diagnostics.Debug.Assert(theType.IsSubClassOf(typeof(BaseClass))); object o = theType.Assembly.CreateInstance(theType.FullName); return (BaseClass)o;}
I'm curious as well actually, I have a plugin system written in C# that the first method and all works fine.
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.
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. :)
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]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement