C#: Dynamic data types?

Started by
6 comments, last by invicticide 19 years, 7 months ago
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.
Advertisement
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));
"There is only one everything"
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 practicepublic BaseClass MakeObject(System.Type theType) {	System.Diagnostics.Debug.Assert(theType.IsSubClassOf(typeof(BaseClass)));	object o = theType.Assembly.CreateInstance(theType.FullName);	return (BaseClass)o;}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

hm. Both ways work for me. :)

Is there any reason to prefer one over the other?
"There is only one everything"
I'm curious as well actually, I have a plugin system written in C# that the first method and all works fine.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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. :)
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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]

This topic is closed to new replies.

Advertisement