Subject: .NET Generics and Dynamic Type Assignment

G

Guest

I have a simple situation in which I want to use generics along with dynamic
type assignment. Following code snippet can explain in more detail
But I am unable to do that will anybody help me why?

namespace genericTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Type X = System.Type.GetType("System.Int32");
GetMyTypes<X> a = new GetMyTypes<X>();
a.MyTypes = "Going out";
MessageBox.Show(a.MyTypes.ToString());

}
}
public class GetMyTypes<T>
{
T t;
public T MyTypes
{
get { return t; }
set { t = value; }
}
}
}


Errors
Error 1 The type or namespace name 'X' could not be found (are you missing a
using directive or an assembly reference?)
 
J

Jon Skeet [C# MVP]

nettellect said:
I have a simple situation in which I want to use generics along with dynamic
type assignment. Following code snippet can explain in more detail
But I am unable to do that will anybody help me why?

To make a generic type with a type only known at runtime, you need to
get the generic version of the type, then call Type.MakeGenericType.

However, at that point it's pretty hard to use.

What are you actually trying to do?
 
G

Guest

well I will try to do it... I am trying to develop some sort of rules
designer in WF using winfx 3.0 so a situation came where I was in need of
this solution to keep my code simple and readable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top