Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Subject: .NET Generics and Dynamic Type Assignment

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nettellect - 28 Feb 2007 14:31 GMT
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?)
Jon Skeet [C# MVP] - 28 Feb 2007 14:43 GMT
> 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?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

nettellect - 28 Feb 2007 15:19 GMT
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.

> > 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
[quoted text clipped - 6 lines]
>
> What are you actually trying to do?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.