PC Review


Reply
Thread Tools Rate Thread

Creating T From Type

 
 
sternr
Guest
Posts: n/a
 
      12th Mar 2007
Hey,
I have the following class:

public Child<T>
{
//Some Code
}

Can I (using Reflection or any other way) create a Child object in
runtime without knowing what is T? like this:
static object GetChild(Type t)
{

}

Thanks ahead

--sternr

 
Reply With Quote
 
 
 
 
Andy
Guest
Posts: n/a
 
      12th Mar 2007
On Mar 12, 11:05 am, "sternr" <Ste...@gmail.com> wrote:
> Hey,
> I have the following class:
>
> public Child<T>
> {
> //Some Code
>
> }
>
> Can I (using Reflection or any other way) create a Child object in
> runtime without knowing what is T? like this:
> static object GetChild(Type t)
> {
>
> }


I think you'll have to use
static object GetChild<T>() : where T : new()

 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      12th Mar 2007
Hello sternr,

[TestFixture]
public class Tests {
[Test]
public void Test( ) {
Assert.AreEqual(typeof(MyClass<string>),
CreateTypeForType(typeof(string)).GetType());
}

private object CreateTypeForType(Type genericParameter) {
Type openType = typeof(MyClass<>);
Type finalType = openType.MakeGenericType(genericParameter);
return Activator.CreateInstance(finalType);
}
}

public class MyClass<T> {
}


Oliver Sturm
--
http://www.sturmnet.org/blog
 
Reply With Quote
 
sternr
Guest
Posts: n/a
 
      12th Mar 2007
On Mar 12, 5:39 pm, "Oliver Sturm" <oli...@sturmnet.org> wrote:
> Hello sternr,
>
> [TestFixture]
> public class Tests {
> [Test]
> public void Test( ) {
> Assert.AreEqual(typeof(MyClass<string>),
> CreateTypeForType(typeof(string)).GetType());
> }
>
> private object CreateTypeForType(Type genericParameter) {
> Type openType = typeof(MyClass<>);
> Type finalType = openType.MakeGenericType(genericParameter);
> return Activator.CreateInstance(finalType);
> }
> }
>
> public class MyClass<T> {
> }
>
> Oliver Sturm
> --http://www.sturmnet.org/blog


Brilliant piece of code!
Thanks a lot Oliver!!!

--sternr

 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      13th Mar 2007
Hello sternr,

>Brilliant piece of code!
>Thanks a lot Oliver!!!


Thank you, I hope it works for you.


Oliver Sturm
--
http://www.sturmnet.org/blog
 
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
Creating List<Type> from type name Andrus Microsoft C# .NET 1 21st Jan 2008 03:55 PM
creating a value type by reflection farseer Microsoft VB .NET 6 20th Oct 2005 02:31 PM
creating a value type by reflection farseer Microsoft C# .NET 1 19th Oct 2005 08:00 AM
Creating a mime type Eric Windows XP Internet Explorer 0 27th Aug 2004 07:09 PM
Cast from type 'DBNull' to type 'String' is not valid. when creating dataset troutbum Microsoft ADO .NET 5 13th Mar 2004 01:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:43 PM.