473,387 Members | 1,700 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,387 software developers and data experts.

Why won't generic accept type param?

The following works fine; passing a type to CreateInstance:
private Type _next;
BaseWizardForm nextForm = (BaseWizardForm)Activator.CreateInstance(
_next );

So why doesn't this work (error = _next' is a 'field' but is used like a
'type'):
BaseWizardForm nextForm =
(BaseWizardForm)Activator.CreateInstance<_next>();

_next *is* a type...

And this gives the error "type or namespace name 'corndog' could not be
found.":
Type corndog = typeof(WelcomeForm);
BaseWizardForm nextForm =
(BaseWizardForm)Activator.CreateInstance<corndog>( );

It gives that error on the second line...
Sep 7 '06 #1
7 1844
>_next *is* a type...

Actually it's an instance of System.Type, something only known at
runtime. That's not what the compiler considers to be a type. So you
have to use Reflection to construct the generic type at runtime.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 8 '06 #2
Brad Wood <du**@sweet.comwrote:
The following works fine; passing a type to CreateInstance:
private Type _next;
BaseWizardForm nextForm = (BaseWizardForm)Activator.CreateInstance(
_next );

So why doesn't this work (error = _next' is a 'field' but is used like a
'type'):
BaseWizardForm nextForm =
(BaseWizardForm)Activator.CreateInstance<_next>();

_next *is* a type...

And this gives the error "type or namespace name 'corndog' could not be
found.":
Type corndog = typeof(WelcomeForm);
BaseWizardForm nextForm =
(BaseWizardForm)Activator.CreateInstance<corndog>( );

It gives that error on the second line...
You're trying to give information at runtime which the compiler needs
at compile time. It's like trying to use:

Type corndog = typeof(WelcomeForm);
corndog f = new corndog();

The compiler doesn't know what type you're talking about, so it can't
do the *compile-time* checks/references it needs to.

--
Jon Skeet - <sk***@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
Sep 8 '06 #3
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
You're trying to give information at runtime which the compiler needs
at compile time. It's like trying to use:

Type corndog = typeof(WelcomeForm);
corndog f = new corndog();

The compiler doesn't know what type you're talking about, so it can't
do the *compile-time* checks/references it needs to.
This is my understanding of how it works (correct me if i'm wrong). If you
use a generic class many times but only with int and string then the
compiler will compile 2 seperate copies of the class, one as if the
appropriate fields/params etc had been defined as int and the other as if
they had been defined as string. Because of this it can't take the type at
runtime.

Michaek
Sep 8 '06 #4
Michael C wrote:
You're trying to give information at runtime which the compiler needs
at compile time. It's like trying to use:

Type corndog = typeof(WelcomeForm);
corndog f = new corndog();

The compiler doesn't know what type you're talking about, so it can't
do the *compile-time* checks/references it needs to.

This is my understanding of how it works (correct me if i'm wrong). If you
use a generic class many times but only with int and string then the
compiler will compile 2 seperate copies of the class, one as if the
appropriate fields/params etc had been defined as int and the other as if
they had been defined as string. Because of this it can't take the type at
runtime.
No, the compiler doesn't create the copies of the class - the CLR does,
and indeed you *can* create generic classes using reflection at
runtime. The problem is that the compiler wants to know about the type
at compile-time - it just doesn't have enough information to work on in
the above scenario.

Jon

Sep 8 '06 #5
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:eV**************@TK2MSFTNGP04.phx.gbl...
_next *is* a type...

Actually it's an instance of System.Type, something only known at
runtime. That's not what the compiler considers to be a type. So you
have to use Reflection to construct the generic type at runtime.
Then I have no idea why Activator has a generic overload. Or maybe I'm just
completely confused. It seems you would always want to pass to Activator a
Type variable rather than explictly referring to the class name. If you
know the class name at compile time, you would just use it's constructor,
not Activator. Right?
Sep 8 '06 #6
Brad,
>Then I have no idea why Activator has a generic overload. Or maybe I'm just
completely confused. It seems you would always want to pass to Activator a
Type variable rather than explictly referring to the class name. If you
know the class name at compile time, you would just use it's constructor,
not Activator. Right?
One reason for the generic Activator.CreateInstance method is in fact
to implement the new operator. This code

class Foo<Twhere T : new() {
T t = new T();
}

compiles to

class Foo<Twhere T : new() {
T t = Activator.CreateInstance<T>();
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 8 '06 #7
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:eM**************@TK2MSFTNGP05.phx.gbl...
One reason for the generic Activator.CreateInstance method is in fact
to implement the new operator. This code

class Foo<Twhere T : new() {
T t = new T();
}

compiles to

class Foo<Twhere T : new() {
T t = Activator.CreateInstance<T>();
}
I think it took this example for the light bulb to go off in realizing the
difference between compile time substitiution of generic arguments and run
time evaluation of type variables...
Sep 8 '06 #8

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

Similar topics

5
by: PJ | last post by:
I have a class defined as so: public class Pager : Control { private PagingList<T> itemList; .... I'm getting an error message that says, "The type or namespace name 'T' could not be found....
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
8
by: kasper.rung | last post by:
I have a problem formulating a test to see if an object is implementing a generic interface. I have the following: public interface IGeneric<T> { void Foo(); }
7
by: Andrus | last post by:
public class BusinessObjectGeneric<EntityType: BusinessObject where EntityType : BusinessEntity, new() { public BusinessObjectGeneric<EntityType() { } ..... causes error in constructor...
6
by: Andrus | last post by:
I need to create generic table field level cache. Table primary key (PrimaryKeyStructType) can be int, string or struct containing int and string fields. FieldName contains table field name to be...
15
by: Anthony Paul | last post by:
Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about creating the following...
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.