473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can a type be instantiated at runtime?

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#?
----------
Will Pittenger
E-Mail: mailto:will.pittenger1 at gmail.com
All mail filtered by Qurb (www.qurb.com)
Nov 17 '05 #1
5 1234

"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
Nov 17 '05 #2
Sounds like you're after the CreateInstance method of the Activator class...
http://msdn.microsoft.com/library/de...tancetopic.asp

--
Tim Wilson
..Net Compact Framework MVP

"Will Pittenger" <se***********@see.signature.net> wrote in message
news:eU**************@TK2MSFTNGP14.phx.gbl...
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#?
----------
Will Pittenger
E-Mail: mailto:will.pittenger1 at gmail.com
All mail filtered by Qurb (www.qurb.com)

Nov 17 '05 #3
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

Nov 17 '05 #4
Thanks anyway, but as soon as I posted, I realized that passing the type did
not make sense. I tried to cancel the post, but I guess you saw it anyway.
----------
Will Pittenger
E-Mail: mailto:will.pittenger1 at gmail.com
All mail filtered by Qurb (www.qurb.com)
Nov 17 '05 #5
I should note that I still need an answer to my "Adding cursor resource to
project" post. If you know of a solution, please respond to that thread.
----------
Will Pittenger
E-Mail: mailto:will.pittenger1 at gmail.com
All mail filtered by Qurb (www.qurb.com)
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rob Long | last post by:
Hey I've noticed a somewhat annoying feature in PHP5 when it comes to extended classes and types. If I declare an interface and a class that implements it, I get a "compile" error (read parse...
7
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
11
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
1
by: Ben Fidge | last post by:
In C# how would I create an instance of a given type at runtime without knowing the exact class at designtime? For example, if I had a factory class that instantiated objects derived from a...
2
by: Anthony Borla | last post by:
Greetings, I hope everyone is enjoying the Holiday Season :) ! I'm attempting to implement a function template modelled somewhat on the STL's, 'generate' and 'generate_n' algorithms. Now,...
5
by: Big Blue Ox | last post by:
Hello, I am trying to store different types that are all derived from the same base class in a list template. So for instance lets say I have this is fragment of skeleton code: class Pet {...
9
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using...
5
by: [rob desbois] | last post by:
Consider the following code: try { // throws exceptions of several types Foo(); } catch (out_of_range& ex) { } catch (exception& ex)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.