PC Review


Reply
Thread Tools Rate Thread

creating new instance though reflection

 
 
bowser
Guest
Posts: n/a
 
      22nd Dec 2006
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 stuff, please let me
know.

My problem is that I have to create a new instance of a class, given a
type and a name (object's id) as parameters. The constructor of such
type has no parameters.

Type t;
string id;
ConstructorInfo ci = t.GetConstructor(new Type[] { });
ci.Invoke(null); // this creates the instance but how to bind it to id?
// I know there's a class named Binder, but how do
I have to use it?

Obviously, trying:
t id = ci.Invoke(null);
is not correct for 2 reasons: t is used as a type, while it's a
variable; secondly id is a string, not an identifier.

I tried also Activator.CreateInstance(m_class): what's the different
between this and the previous way?

Finally I need to know if the id (that I have as string variable), is
already binding another object, so that I can change it to do my work.

Thank you in advance.

Alessandro

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Dec 2006
"binding" doesn't apply here... can you clarify what you mean with the
"id is a string not an identifier"? This might mean you need to find
the field first...

ci.Invoke() should return the instance, but unless you are using
generics, all you can do is treat it as an object, i.e. object id =
ci.Invoke(null);

However, if you are talking about a paramererless constructor, then in
2.0 generics is the answer:

public static void SomeMethod<T>() where T : new() {
// do some work
T t = new T();
// do something with T...
}

of course, in the above you can't do much with t by itself, unless you
a: are putting it into an existing collection of Ts, or b: you know
that T supports certain features:

public static void SomeMethod<T>(IList<T> list) where T : new(),
ISomeInterface {
T t = new T();
list.Add(t);
t.SomeMethod();
}

If I understand your intent ("id as a string"), then another trick
might be:

public static void RecreateField<T>(ref T field) where T : new() {
field = new T();
}

This then allows you to call things like
"RecreateField(someTypedField);" and it will just work... of course,
unless you are doing something else in that code block it would be
easier to just type "someTypedField = new FieldType();"... so can you
clarify?

Marc


 
Reply With Quote
 
=?Utf-8?B?Q2lhcmFuIE8nJycnRG9ubmVsbA==?=
Guest
Posts: n/a
 
      22nd Dec 2006
If you want to assign a class level field named with the contents of the id
string then you will need to call something like this:

Type t;
string id;
ConstructorInfo ci = t.GetConstructor(new Type[] { });
object newob = ci.Invoke(null);
this.GetType().GetField(Id, System.Reflection.BindingFlags.FlattenHierarchy
| System.Reflection.BindingFlags.Instance).SetValue(this,newob);
--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com


"bowser" wrote:

> 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 stuff, please let me
> know.
>
> My problem is that I have to create a new instance of a class, given a
> type and a name (object's id) as parameters. The constructor of such
> type has no parameters.
>
> Type t;
> string id;
> ConstructorInfo ci = t.GetConstructor(new Type[] { });
> ci.Invoke(null); // this creates the instance but how to bind it to id?
> // I know there's a class named Binder, but how do
> I have to use it?
>
> Obviously, trying:
> t id = ci.Invoke(null);
> is not correct for 2 reasons: t is used as a type, while it's a
> variable; secondly id is a string, not an identifier.
>
> I tried also Activator.CreateInstance(m_class): what's the different
> between this and the previous way?
>
> Finally I need to know if the id (that I have as string variable), is
> already binding another object, so that I can change it to do my work.
>
> Thank you in advance.
>
> Alessandro
>
>

 
Reply With Quote
 
bowser
Guest
Posts: n/a
 
      22nd Dec 2006
Marc,

object id = ci.Invoke(null);

is incorrect because the type of 'id' is 'string'. (The compiler says:
"A local variable named 'id' cannot be declared in this scope because
it would give a different meaning to 'id', which is already used in a
'parent or current' scope to denote something else")

But that would be exactly what I wanted to do: create an object whose
name is the value of 'id'. For example if id="foo" and t="Foo", I want
to create an object 'Foo foo', that is: through reflection, I want to
get the equivalent of
Foo foo = Foo();

The right member ('Foo();') is translated as ...'ci.Invoke(null);'
but the left member remains a problem for me.

I hope I've been clear this time.

Thank you very much.

Alessandro

 
Reply With Quote
 
bowser
Guest
Posts: n/a
 
      22nd Dec 2006
In the example I meant
Foo foo = new Foo();

I want to create the instance in a particular namespace, say 'ns',
which is not the one where I put the code under discussion.

Finally I recall the second problem: to check if 'foo' already exist
(whatever it's its type) given a namespace.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using reflection to get instance name =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?= Microsoft Dot NET 2 9th May 2007 04:45 PM
reflection: problem creating new instance bowser Microsoft C# .NET 1 3rd Jan 2007 07:01 PM
Creating copy of structure instance via reflection? Larry Minton Microsoft VC .NET 1 24th May 2006 09:05 AM
creating delegate instance from method retrieved through reflection Pieter Breed Microsoft C# .NET 1 1st Sep 2004 03:27 PM
Creating an instance of a no-constructor-struct with reflection Carl Rosenberger Microsoft C# .NET 2 28th Nov 2003 05:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:24 AM.