Please help me to look at the following code:
-------------start----------
using System;
using System.Collecti ons.Generic;
public class GenType
{
}
public class GenericType1<Tw here T : new()
{
public static T Instance
{
get
{
return new T();
}
}
protected static string SECTION_ID;
}
public class GenericType2<T>
: GenericType1<Tw here T : new()
{
public static T Load()
{
return Instance;
}
}
public sealed class Alpha
: GenericType2<Ge nType>
{
static Alpha()
{
Console.WriteLi ne("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? 3 2282
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.MyStati cMethod will be compiled
as Base.MyStaticMe thod - 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<Ge nTypeand changed SECTION_ID, you'd have problems.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
On Jul 24, 6:21 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
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.MyStati cMethod will be compiled
as Base.MyStaticMe thod - 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<Ge nType, "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<Ge nTypeand changed SECTION_ID, you'd have problems.
thanks for reminding.
>
--
Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
On Jul 24, 6:21 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
>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.MyStati cMethod will be compiled as Base.MyStaticMe thod - 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<Ge nType, "my id">
I am guessing this is not something DOTNET generic would like to do.
No, you can't do that.
Jon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 it to be able to take in an instance of the generic
REGARDLESS of what the "Of"
type of the generic is.
E.g. .... given a generic class MyGen(Of T)
I want to be able to write another class as such:
|
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 subclass of
Logical (LogicalA, LogicalB). I'm currently casting them to the right
type when I retrieve them, but obviously this isn't terribly 'nice'.
I tried converting the class to be generic, and having a type parameter
that affected the type of...
|
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 implements IQueue<Tfor
all types T (so it can make use of queues of various object types
internally). As useful as this is, it doesn't seem possible. The natural (but
illegal) notation would be something like
class A<QueueClasswhere QueueClass :...
|
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 application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The problem I'm
running into has to do with the differences in implementations of
Generics between the two...
|
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 the type U depending on the type T. As
| |
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 I will need the Generic Class, given there are so many other
options.
--
cheers,
|
by: Dave |
last post by:
I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegate;
instantiation:
displayMsgDelegate = DisplayStatusMessage;
implementation:
public void DisplayStatusMessage(string statusMessage)
|
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 generic list
of business objects. I'm able to see that the type is the correct one
in the debugger.
However when I try to traverse the list using the type I can't
compile. The same type variable I've verified as being passed
|
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 delegate
type expression in just the right place and a well-named method means
we can almost read the code out loud and understand it without even
thinking.
Note that since 'boxing' and 'unboxing' is involved (I think), you
don't get what you...
|
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 types derived from that base type, or arrays or generic collections of instances of types derived from that base type.
All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |