Luc,
One way might be to cast the value to an object, then cast it to T,
something like:
| if (valeurType.GetType() == typeof(Byte))
| valeurType = (T)(object)Byte.MinValue;
If you know you want "MinValue" I would consider using Reflection to find
the "MinValue" field on the respective type, something like:
private void InitializeValue(out T value)
{
Type t = typeof(T);
System.Reflection.FieldInfo fi = t.GetField("MinValue");
if (fi == null)
{
value = default(T);
}
else
{
value = (T)fi.GetValue(null);
}
}
This helps ensure that C<T> supports "MinValue" on new types that may be
used with it, without modifying C<T> itself.
NOTE: The above routine assumes that MinValue is a static field...
Although I would define the class such that default(T) was acceptable or
used another field to indicate the special value...
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
|I have already tried that, but it didn't work (for the same reasons):
|
| public class C<T>
| {
| private T myValue;
|
| private void InitializeValue(out T value)
| {
| if (valeurType.GetType() == typeof(Byte))
| valeurType = Byte.MinValue;
| else if (valeurType.GetType() == typeof(Char))
| valeurType = Char.MinValue;
| else if (valeurType.GetType() == typeof(Int16))
| valeurType = Int16.MinValue;
| // and so on...
| else
| valeurType = default(T);
| }
|
| public C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
| }
|
| Error messages are :
| Error 1 Cannot implicitly convert type 'byte' to 'T'
| Error 2 Cannot implicitly convert type 'char' to 'T'
| Error 3 Cannot implicitly convert type 'short' to 'T'
|
| and so on...
|
|
|
| "Nicholas Paldino [.NET/C# MVP]" wrote:
|
| > Then you will have to do a type comparison on T and then set your
value
| > based on the type of T.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - (e-mail address removed)
| >
| > | > > Yes, this is for the default value, but what if I want to initialize
my
| > > variable with a value that is not the default one ?
| > >
| > > "Joanna Carter [TeamB]" wrote:
| > >
| > >> "Luc Vaillant" <
[email protected]> a écrit dans
le
| > >> message de (e-mail address removed)...
| > >>
| > >> |I need to initialise a typed parameter depending of its type in a
| > >> generic
| > >> | class.
| > >> | I have tried to use the C++ template form as follow, but it doesn't
| > >> work.
| > >> | It seems to be a limitation of generics vs C++ templates.
| > >> | Does anyone knows a workaround to do this ? Thx :
| > >> |
| > >> | public class C<T>
| > >> | {
| > >> | private T myValue;
| > >> |
| > >> | private void InitializeValue(out Byte value) { value =
| > >> Byte.MinValue; }
| > >> | private void InitializeValue(out Char value) { value =
| > >> Char.MinValue; }
| > >> | private void InitializeValue(out Int16 value) { value =
| > >> Int16.MinValue; }
| > >> | // ... and so on...
| > >> | private void InitializeValue(out String value) { value = "Initial
| > >> value"; }
| > >> | private void InitializeValue(out Object value) { value = this; }
| > >> |
| > >> | public C()
| > >> | {
| > >> | InitializeValue(out myValue);
| > >> | }
| > >> |
| > >> | // ...
| > >> | }
| > >>
| > >> You are not specifying the type that you wish to initialise, to the
| > >> constructor.
| > >>
| > >> In any case, you can use the 'default' keyword to initialise any type
| > >> passed
| > >> as the parameter to the generic class :Then you don't even have to
| > >> declare a
| > >> constructor for this purpose.
| > >>
| > >> public class C<T>
| > >> {
| > >> private T myValue = default(T);
| > >> }
| > >>
| > >> Joanna
| > >>
| > >> --
| > >> Joanna Carter [TeamB]
| > >> Consultant Software Engineer
| > >>
| > >>
| > >>
| >
| >
| >