473,406 Members | 2,273 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,406 software developers and data experts.

System.Type parameter

I am passing a System.Type parameter into a method. I would like to create an object of that type from the System.Type.

for example

public CreateAnObject( System.Type objectType)
{
m_MemberObject = new "The class represented by objectType";
}

and called by

CreateAnObject(typeof(someClass));

Underlying reason is I want to pass a class in a custom attribute so I cannot pass an instantiated object (must be a class or a constant)

Thanks

Martin
Nov 16 '05 #1
3 10347

"Martin Montgomery" <an*******@discussions.microsoft.com> skrev i en
meddelelse news:7A**********************************@microsof t.com...
I am passing a System.Type parameter into a method. I would like to create an object of that type from the System.Type.
for example

public CreateAnObject( System.Type objectType)
{
m_MemberObject = new "The class represented by objectType";
}

[Snip]

Something like;
public void CreateAnObject( System.Type objectType)
{
object o = Activator.CreateInstance(objectType);
// do other stuff, and sure to put above into a try-catch statement
}

Soren
Nov 16 '05 #2
Martin,

You can pass the type to the static CreateInstance method on the
Activator class. This will allow you to create an instance based on the
type. However, you have to know if the type constructor takes parameters or
not, and if it does, you have to supply them.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Martin Montgomery" <an*******@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
I am passing a System.Type parameter into a method. I would like to create an object of that type from the System.Type.
for example

public CreateAnObject( System.Type objectType)
{
m_MemberObject = new "The class represented by objectType";
}

and called by

CreateAnObject(typeof(someClass));

Underlying reason is I want to pass a class in a custom attribute so I cannot pass an instantiated object (must be a class or a constant)
Thanks

Martin

Nov 16 '05 #3
Hi Martin,

Hi Martin,
In addition to the solution Nicholas and Soren gave you I would add that you
can use Type.GetConstructor to obtain the construcotr you want to use.
Having ConstructorInfo object you can call Invoke method to create an
instance of the type.
--

Stoitcho Goutsev (100) [C# MVP]
"Martin Montgomery" <an*******@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
I am passing a System.Type parameter into a method. I would like to create an object of that type from the System.Type.
for example

public CreateAnObject( System.Type objectType)
{
m_MemberObject = new "The class represented by objectType";
}

and called by

CreateAnObject(typeof(someClass));

Underlying reason is I want to pass a class in a custom attribute so I cannot pass an instantiated object (must be a class or a constant)
Thanks

Martin

Nov 16 '05 #4

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

Similar topics

2
by: Chris Aitchison | last post by:
Hello, I am attempting to have a class that I have written serialize so that it can be both passed as a parameter or return value for a webservice, and also be serialized to disk using the...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
0
by: DC | last post by:
Why would this code give me the error "Could not load type System.Web.UI.WebControls.AccessDataSource from assembly System.Web, Version=1.0.5000.0" at line 16... <form id="form1"...
5
by: Michael Primeaux | last post by:
I have a simple .NET 2.0 web service created with VS.NET 2005 with a single web method with the following signature: void HelloWorld(Guid parameter1); When calling this method I receive the...
8
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I'm trying to pass values of different data-types to a web-service. I thought it would be easier to box these values and pass them as a System.object parameter, like public void...
0
by: jammendolia | last post by:
Hello all, I am having an intermittent exception occur when writing the the eventlog from a windows service application written in c#, running on a Win2k SP4 server. Here's the code: ...
2
by: BruceWho | last post by:
I downloaded boost1.35.0 and built it with following command: bjam --toolset=msvc-7.1 --variant=release --threading=multi -- link=shared --with-system stage and it failed to compile, error...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.