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

Create an instance from type as string

Hi all,

Real easy one I think, just can't find example,

I need to create an instance of a type, but i don't have a type var, i have
a string
e.g.
string t = "MyNamespace.Class1";

I wan't to do something like
System.Activator.CreateInstance(t); // i know the relevant assemblies will
be loaded.

Obviously i know this createinstance method takes a System.Type parameter,
not a string representation.

any ideas?

TIA

Sam Martin
Nov 15 '05 #1
3 28116
boxim <sa*********@yahoo.co.uk> wrote:
Real easy one I think, just can't find example,

I need to create an instance of a type, but i don't have a type var, i have
a string
e.g.
string t = "MyNamespace.Class1";

I wan't to do something like
System.Activator.CreateInstance(t); // i know the relevant assemblies will
be loaded.

Obviously i know this createinstance method takes a System.Type parameter,
not a string representation.


No it doesn't - or rather, not all overloads of it do. There's a perfectly
usable Activator.CreateInstance(string, string) where you give the
assembly name and the type name.

Alternatively, use Type.GetType to get the type if it's in either
mscorlib or your own assembly. Otherwise, load the relevant assembly
and then use Assembly.GetType.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
ok, thanks

got it working, now having to determine the constructor to use (if no
paramterless one available), and pass corrent values, etc.

however....

how do i compare to types

(Runtime Type) System.Char* and System.String?

Maybe if you can answer why if can't do this it would help....

object myobj =
System.Activator.CreateInstance(Type.GetType("Syst em.String"),new object[]
{"string value"});

It's throwing a missing method exception, I'm guessing because there is no
contructor that takes a string - only takes a System.Char*.
There is no default (parameterless contructor) .

To use System.Char* I'd have to unsafe it, but surely there's a way of doing
it passing a string?

TIA

Sam Martin
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
boxim <sa*********@yahoo.co.uk> wrote:
Real easy one I think, just can't find example,

I need to create an instance of a type, but i don't have a type var, i have a string
e.g.
string t = "MyNamespace.Class1";

I wan't to do something like
System.Activator.CreateInstance(t); // i know the relevant assemblies will be loaded.

Obviously i know this createinstance method takes a System.Type parameter, not a string representation.


No it doesn't - or rather, not all overloads of it do. There's a perfectly
usable Activator.CreateInstance(string, string) where you give the
assembly name and the type name.

Alternatively, use Type.GetType to get the type if it's in either
mscorlib or your own assembly. Otherwise, load the relevant assembly
and then use Assembly.GetType.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
boxim <sa*********@yahoo.co.uk> wrote:
got it working, now having to determine the constructor to use (if no
paramterless one available), and pass corrent values, etc.

however....

how do i compare to types

(Runtime Type) System.Char* and System.String?
They're not the same, basically.
Maybe if you can answer why if can't do this it would help....

object myobj =
System.Activator.CreateInstance(Type.GetType("Syst em.String"),new object[]
{"string value"});
You can't do that because there's no constructor for String which takes
another String.
It's throwing a missing method exception, I'm guessing because there is no
contructor that takes a string - only takes a System.Char*.
Yup. And char[], amongst others.
There is no default (parameterless contructor) .

To use System.Char* I'd have to unsafe it, but surely there's a way of doing
it passing a string?


If you've already *got* the string value, why do you want to create
another string with the same data? Just use the original string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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

Similar topics

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...
6
by: Rene Mansveld | last post by:
Hi, how can I create an instance (object) of a class (form) if I only know the classname (VB.NET 1.0)? I need to do this in a complex app where jobs consist of parts. Each part's data is saved...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
8
by: Bert | last post by:
Hi, I want to create an instance of a class by means of the classname. Can't figure out how. in pseudocode this would look like: Dim MyClassName as String = "MyClass" Dim MyObject as New...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
13
by: Bill Nguyen | last post by:
Is it possible to create your won XSD to use with .NET based on an XML content? For example the one below: <?xml version="1.0"?> <pcats:FuelsDoc...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
9
by: Grant Schenck | last post by:
Hello, I have a base class with several derived classes: ScriptBase + ScriptCallInbound + ScriptCallOutbound I then have another class: Line
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.