473,386 Members | 1,720 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,386 software developers and data experts.

reflection: problem creating new instance

Hello, I have a problem with reflection. I posted it already a month
ago, but I had no luck with the answers...

Having two variables:
Type t;
string id;

initialized for example as t="Foo" and id="foo"

I need to make the equivalent of :
Foo foo = new Foo();

meaning, something like:
t id = new t();

(but obviously this is incorrect since id is a string and t is a
variable and can't be used for this purpose in this way).
I know how to instantiate the new object:
ConstructorInfo ci = t.GetConstructor(new Type[] { });
object obj = ci.Invoke(null);

But I need to assign name and type to it.
How can I do?

Thank you.

Alesssandro

Jan 3 '07 #1
1 4462
Hi
I don;t quite understand your post, but I think that you want to create an
instance of a class that you only know by name. If this is the case there
are a few way that you can do so.

If you know the assembly (and have a reference to it) where the class is
defined you can use Assembly.CreateInstance( string type)

There are other classes that are capable to create instances (AppDomain,
etc) take a look at CreateInstance method in MSDN and it will list all those
classes

Also note that you need to use the full name of the type, instead of "Foo"
you need to use "MyAssembly.Foo"
--
Ignacio Machin
machin AT laceupsolutions com

"bowser" <al****************@gmail.comwrote in message
news:11**********************@k21g2000cwa.googlegr oups.com...
Hello, I have a problem with reflection. I posted it already a month
ago, but I had no luck with the answers...

Having two variables:
Type t;
string id;

initialized for example as t="Foo" and id="foo"

I need to make the equivalent of :
Foo foo = new Foo();

meaning, something like:
t id = new t();

(but obviously this is incorrect since id is a string and t is a
variable and can't be used for this purpose in this way).
I know how to instantiate the new object:
ConstructorInfo ci = t.GetConstructor(new Type[] { });
object obj = ci.Invoke(null);

But I need to assign name and type to it.
How can I do?

Thank you.

Alesssandro

Jan 3 '07 #2

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

Similar topics

6
by: kapilp | last post by:
hi, i have a program that used reflection to execute methods. now i want to execute the reflected method on a new thread but cant figure out how or if it can be done. take the below code, for...
2
by: Carl Rosenberger | last post by:
Hi all, I would like to create an instance of a struct with reflection. The way I am used to doing this with classes: - get a ConstructorInfo from the Type - call Invoke() However, if a...
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
3
by: Bisbal | last post by:
Hi, I'd like to know if it is possible to create a form type from a string. My case: I have a DataTable with forms names (i.e frm_001_UserInfo.cs), and I'd like to create an instance of that...
3
by: System.Reflection Activator | last post by:
************************************** //Load the Assembly Assembly a = Assembly.LoadFrom(sAssembly); //Get Types so we can Identify the Interface. Type mytypes = a.GetTypes(); BindingFlags...
9
by: TS | last post by:
i have code that creates assemblies and classes from the assemlby and methods from the classes to set properties of dynamically created controls. How i should go about validating the assemblies,...
4
by: Andre Nogueira | last post by:
Hi guys I am developing a plugin-enabled application, and for that I am using reflection. I created an abstract ("MustInherit") class, from which all plugins must inherit. My question is......
4
by: bowser | last post by:
Hello, I'm new to C# but so far I didn't find big problems, except for Reflection. I coudn't find any good material in internet where to study it. If anyone knows about some documents, blogs or...
7
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
Say I have a class that has a generics List as follows: public List<MyClassmyClassList = new List<MyClass>(); and I want to create another class which tries to add an element of MyClass to...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
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: 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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.