Going beyond what was asked....
Its a often programming situation these days that we might need to create an
object based off a string (ie an attribute in an xml file). This is also
easy to do,
Stiring sType = "{classname}, {assemblyname}";
Type type = Type.GetType(sType);
object x = Activator.CreateInstance(type);
Joe
MCAD
SRE (Simple Rule Engine)
https://sourceforge.net/projects/sdsre/
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
"Will Pittenger" wrote...
I have a function that I want to be passed a System.Type. If the type is
derived from a specific class, I want to have the function instantiate an
instance. With MFC, this would be a snap (provided the class was created
with the DYNCREATE macros).
Can this be done in C#?
Sure...
object GetInstance(Type type)
{
return Activator.CreateInstance(t);
}
However, you might need to look closer to the reflection in .NET, as you
might go into some traps where some types doesn't have default
constructors, etc.
// Bjorn A