473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

.NET Generics and Dynamic Type Assignment

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<Xa = 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?)
Feb 28 '07 #1
2 5337
nettellect wrote:
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<Xa = 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?)
Using generics always has to resolve to compile time correct code,
i.e. the generic parameters have to be known at compile time. Your code
doesn't obey that, 'x' is known at runtime. That's not going to work.

So if you know 'x' at compile time (and your code does, it's an int),
specify X as int.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Mar 1 '07 #2
Agreed but what is the solution... instead of makeGenericType function

"Frans Bouma [C# MVP]" wrote:
nettellect wrote:
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<Xa = 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?)

Using generics always has to resolve to compile time correct code,
i.e. the generic parameters have to be known at compile time. Your code
doesn't obey that, 'x' is known at runtime. That's not going to work.

So if you know 'x' at compile time (and your code does, it's an int),
specify X as int.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Mar 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vinodh Kumar P | last post by:
Whenever I read any C++ literature I find the words "C++ is statically type checked".OK.Agreed. Is there any language that supports "Dynamic type checking"? In such a case any type can be assigned...
1
by: Jesper | last post by:
Hi, I've done some file type assignment to a program I've made such that e.g. when I double click on files of type .foo it will be openened in my program. However, I'm still missing two...
8
by: Ashish | last post by:
Hi all, I have interface declared like public IBaseInterface { } then a generic collection like
10
by: gk245 | last post by:
I have something like this: #include <stdio.h> main () { struct line { char write; char read;
8
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
1
by: dick | last post by:
how C++ implements the dynamic type?
10
by: Marc Gravell | last post by:
Given a generic method "of T", is there a good way of ensuring that any static ctor on T has executed? Following code demonstrates (TestClass1) that via generics you can use the Type instance long...
2
by: doomer | last post by:
Hello, I'm trying to build a set of classes that act as the handlers for a simple expression parser, and i'm wondering how to preserve the dynamic type information from derived classes. An...
2
by: J. Peng | last post by:
Python's variable is dynamic type,is it? But why this can't work? Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsupported operand type(s) for +: 'int' and 'str' ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.