473,796 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(someClas s));

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 10370

"Martin Montgomery" <an*******@disc ussions.microso ft.com> skrev i en
meddelelse news:7A******** *************** ***********@mic rosoft.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.Creat eInstance(objec tType);
// 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.co m

"Martin Montgomery" <an*******@disc ussions.microso ft.com> wrote in message
news:7A******** *************** ***********@mic rosoft.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(someClas s));

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.GetConstru ctor 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*******@disc ussions.microso ft.com> wrote in message
news:7A******** *************** ***********@mic rosoft.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(someClas s));

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
6827
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 XmlSerializer. When this class is decalred as a return value for a WebMethod in an XML WebService I receive the following exception: System.IO.FileNotFoundException: File or assembly name rexz5r1o.dll, or one
7
9972
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 method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
3
8839
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 don't think I made any changes to either page other than changing the user control that creates the header). Server Error in '/myApp' Application. ---------------------------------------------------------------------------- ----
0
3020
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" runat="server"> Line 15: <div> Line 16: <asp:AccessDataSource ID="AccessDataSource1" Runat="server" DataFile="data\pubs.mdb" Line 17: SelectCommand="select * from "
5
6503
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 following error: System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Web.Services.Protocols.HttpServerType..ctor(Type type) at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
8
8903
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 webmethod1(object a) where a can be of type string, DateTime, float, etc..
0
6101
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: void writeToEventLog(string msg, bool error) {
2
4145
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 message is: E:\software\development\boost_1_35_0\boost_1_35_0>bjam -- toolset=msvc-7.1 --variant=release --threading=multi --link=shared --
0
9683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.