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

C# generic type

Please help me to look at the following code:

-------------start----------
using System;
using System.Collections.Generic;

public class GenType
{
}

public class GenericType1<Twhere T : new()
{
public static T Instance
{
get
{
return new T();
}
}

protected static string SECTION_ID;
}

public class GenericType2<T>
: GenericType1<Twhere T : new()
{
public static T Load()
{
return Instance;
}
}

public sealed class Alpha
: GenericType2<GenType>
{
static Alpha()
{
Console.WriteLine("static ctor called.");
SECTION_ID = "Alpha";
}
}

public class Test
{
public static void Main()
{
GenType o = Alpha.Load();
}
}
-------------end----------

When I run the compiled code, I don't see "static ctor called." in
output (I am not sure why not).

I intend to implement most of the code in base generic class, only
pass some values (e.g. SECTION_ID) to base class from children class
(e.g. Alpha). Instance and Load() have to be static, do you guys have
any solution?

Jul 24 '07 #1
3 2269
webcliff <we******@gmail.comwrote:
Please help me to look at the following code:
<snip>
When I run the compiled code, I don't see "static ctor called." in
output (I am not sure why not).
This is nothing to do with being generic - if you have two classes,
"Base" and "Derived" and a static method in "Base" called
MyStaticMethod, then a call to Derived.MyStaticMethod will be compiled
as Base.MyStaticMethod - which means that the static constructor for
Derived won't get called at that point.
I intend to implement most of the code in base generic class, only
pass some values (e.g. SECTION_ID) to base class from children class
(e.g. Alpha). Instance and Load() have to be static, do you guys have
any solution?
If you want to initialize a whole bunch of classes at a particular
point in time, it's probably best to do so in some other way - such as
by having an attribute on each type that needs to be initialized, and
run through such types with reflection.

Note that you only get one SECTION_ID variable per parameter type
argument used - so if any other class derived (directly or indirectly)
from GenericType1<GenTypeand changed SECTION_ID, you'd have problems.

--
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
Jul 24 '07 #2
On Jul 24, 6:21 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
webcliff <webcl...@gmail.comwrote:
Please help me to look at the following code:

<snip>
When I run the compiled code, I don't see "static ctor called." in
output (I am not sure why not).

This is nothing to do with being generic - if you have two classes,
"Base" and "Derived" and a static method in "Base" called
MyStaticMethod, then a call to Derived.MyStaticMethod will be compiled
as Base.MyStaticMethod - which means that the static constructor for
Derived won't get called at that point.
m... I didn't know that, but I still wonder why the compiler want to
do this,
it is a suprise.
>
I intend to implement most of the code in base generic class, only
pass some values (e.g. SECTION_ID) to base class from children class
(e.g. Alpha). Instance and Load() have to be static, do you guys have
any solution?

If you want to initialize a whole bunch of classes at a particular
point in time, it's probably best to do so in some other way - such as
by having an attribute on each type that needs to be initialized, and
run through such types with reflection.
Classes like Alpha are auto-generated types.
What I really want is something like a parameterized generic type, I
can create
a type (i.e. Alpha) with a type GenType and a string:

public Alpha : GenericType1<GenType, "my id">

I am guessing this is not something DOTNET generic would like to do.

>
Note that you only get one SECTION_ID variable per parameter type
argument used - so if any other class derived (directly or indirectly)
from GenericType1<GenTypeand changed SECTION_ID, you'd have problems.
thanks for reminding.
>
--
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
Jul 25 '07 #3
On Jul 24, 6:21 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
>webcliff <webcl...@gmail.comwrote:
>>Please help me to look at the following code:
<snip>
>>When I run the compiled code, I don't see "static ctor called." in
output (I am not sure why not).
This is nothing to do with being generic - if you have two classes,
"Base" and "Derived" and a static method in "Base" called
MyStaticMethod, then a call to Derived.MyStaticMethod will be compiled
as Base.MyStaticMethod - which means that the static constructor for
Derived won't get called at that point.

m... I didn't know that, but I still wonder why the compiler want to
do this, it is a suprise.
The meaning of "static" is that the complete information is known at
compilation time - it is statically called rather than dynamically
called. The compiler has determined that the static method in the base
class should be called, so that's the code that it emits.
>If you want to initialize a whole bunch of classes at a particular
point in time, it's probably best to do so in some other way - such as
by having an attribute on each type that needs to be initialized, and
run through such types with reflection.

Classes like Alpha are auto-generated types.
In that case you could create a static method in the auto-generated
type
which calls the static method in the base type.
What I really want is something like a parameterized generic type, I
can create
a type (i.e. Alpha) with a type GenType and a string:

public Alpha : GenericType1<GenType, "my id">

I am guessing this is not something DOTNET generic would like to do.
No, you can't do that.

Jon

Jul 25 '07 #4

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

Similar topics

1
by: Arthur Dent | last post by:
Hi all... Heres what im looking to do.... I have a Generic class i wrote. Now, on another class, i want to add a method which can take in an object of my generic class... but the catch is, i want...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
3
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify...
10
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.