PC Review


Reply
Thread Tools Rate Thread

Class instantiation from Type

 
 
tommasop@gmail.com
Guest
Posts: n/a
 
      22nd Jun 2006
I'm developing a custom server control that should take a datasource
and its Type and from its Type instantiate internal objects to display
data.

MyServerComponent mySrv = new MyServerComponent();
mySrv.DataSource = My.NewsManager.GetObjects();
mySrv.ObjectType = typeof(News);

Internally I would like to be able to do something like this:

((ObjectType)myObj).Title = TitleTbox.Text;

Using ObjectType to instantiate, cast objects to actual object class.

Is it possible?

Is there any workaround?

Thanks

Tommaso Patrizi

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Jun 2006
The relationship between your two blocks of code is a little hazy, so...

You could use reflection:
ObjectType.GetProperty("Title").SetValue(myObj, TitleTbox.Text, null);

Alternatively, if you are talking about windows-forms, then you might be
able to usee bindings to attach the "Text" property of the textbox to the
"Title" property of the object, and it will manage reads and writes for you.

Otherwise - do you have multiple objects with a .Title (and the other
properties)? If so, then interfaces are the way to go - if myObj is always
going to be one of these.

Marc


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Jun 2006
Sorry - missed a bit; to instantiate via reflection (using the default
public ctor), you can use:

ObjectType.GetConstructor(Type.EmptyTypes).Invoke(null);

However, in 2.0 another option is to use generics constrained with the new()
condition, e.g.

public void CreateAndBindToObject<T>() where T : new() {
T t = new T();
// do some things with you new T instance
}

e.g. call CreateAndBindToObject<SomeClass>(); note that this won't provide
the properties (.Title etc) unless you for instance also restrict to an
interface:

public void CreateAndBindToObject<T>(string title) where T : ISomeInterface,
new() {
T t = new T();
t.Title = title; // assuming ISomeInterface defines "string Title {set;}"
// do some things with you new T instance
}

Marc


 
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
Instantiation via Class Name Chris Marsh Microsoft Dot NET 4 20th May 2007 11:01 AM
Class Instantiation =?Utf-8?B?U2NvdHQgSC4=?= Microsoft VB .NET 3 11th Apr 2006 02:49 AM
Class Instantiation by name =?Utf-8?B?TWFuZGFy?= Microsoft Dot NET 1 19th Jan 2004 09:25 AM
Dynamic class instantiation Jim Witt Microsoft C# .NET 1 8th Nov 2003 12:36 AM
Instantiation class Peter Krikelis Microsoft C# .NET 1 7th Nov 2003 04:48 PM


Features
 

Advertising
 

Newsgroups
 


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