Initialising a generic parameter in a generic class - c#

Alright, I have a generic class. The basics are this:
public class GenericPrimitiveContainer<T> : IGetAndSetGenericValue
{
private T _value;
public T Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
}
Fine enough. I also have a SetValue method in that class which takes an object and sets it to the value which uses this code:
PropertyInfo pi = this.GetType().GetProperty("Value");
pi.SetValue(this, o, null);
That's not all it does because it also checks the type of the object and the type of Value and compares them. If they are the same, o (the object) is assigned to Value. If they're not the same, a conversion is applied. I won't go into that because that's not where the problem is (famous last words I'm sure).
The problem is if Value is of type string. As I said before, I compare the types to see if they are the same. This is as follows ('o' being the object passed in):
Type t1 = Value.GetType();
Type t2 = o.GetType();
if (t1 == t2) ...
If T is int, no problem. If T is String, 'Value' is just 'null'. I can't do 'GetType' on it even to see if it's a String because it's just null.
I have tried, as a test, just getting rid of the checks and testing the set method in a situation where I know a string will be passed to the method. Value was still initially null, but it worked out okay and Value was assigned.
Now I know string is not a primitive and so will work a little differently to an int, but I'm not sure how to overcome this problem. I considered initialising _value to default(T), which didn't work. I also added a constructor to the class which did the same thing. That also didn't work.
I have also tried constraining the class with a 'where t : new()' but that doesn't work because String is not a "non-abstract type with a public parameterless constructor in order to use it as parameter 'T'".
So hopefully someone wiser can help me out on this one.

Your problem is that Value.GetType() doesn't do what you want. Here's a really short complete example:
using System;
static class Generic
{
public static string WhatIsT<T>(T value)
{
return $"T is {value.GetType().FullName}";
}
}
class Program
{
static void Main(string[] args)
{
int i = 5;
Console.WriteLine(Generic.WhatIsT(i));
string s = "hello";
Console.WriteLine(Generic.WhatIsT(s));
s = null;
Console.WriteLine(Generic.WhatIsT(s));
Console.ReadLine();
}
}
The first and second calls to WhatIsT will be fine, but there will be a null reference exception on the third.
If you really really really really need to know the exact name of the generic type you've been closed over - and please heed the caveats in the comments that this almost certainly isn't the right thing to be doing - simply use typeof(T), like this:
return $"T is {typeof(T).FullName}";
Result:
T is System.Int32
T is System.String
T is System.String
Remember, GetType needs an object. typeof just needs a compile time name, which includes type parameters.

Related

MassTransit. Exclude message types from topology by type object [duplicate]

This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Closed 9 years ago.
I have the following code:
public class ClassExample
{
void DoSomthing<T>(string name, T value)
{
SendToDatabase(name, value);
}
public class ParameterType
{
public readonly string Name;
public readonly Type DisplayType;
public readonly string Value;
public ParameterType(string name, Type type, string value)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
if (type == null)
throw new ArgumentNullException("type");
this.Name = name;
this.DisplayType = type;
this.Value = value;
}
}
public void GetTypes()
{
List<ParameterType> l = report.GetParameterTypes();
foreach (ParameterType p in l)
{
DoSomthing<p.DisplayType>(p.Name, (p.DisplayType)p.Value);
}
}
}
Now, I know I cannot perform DoSomething()
is there any other way to use this function?
You can, but it involves reflection, but you can do it.
typeof(ClassExample)
.GetMethod("DoSomething")
.MakeGenericMethod(p.DisplayType)
.Invoke(this, new object[] { p.Name, p.Value });
This will look at the top of the containing class, get the method info, create a generic method with the appropriate type, then you can call Invoke on it.
this.GetType().GetMethod("DoSomething").MakeGenericMethod(p.Value.GetType()).Invoke(this, new object[]{p.Name, p.Value});
Should work.
Generics types cannot be specified at runtime the way you'd like to here.
The simplest options would be to add a non-generic overload of DoSomething, or simply call DoSomething<object> and ignore p.DisplayType. Unless SendToDatabase depends on the compile-time type of value (and it probably shouldn't), there should be nothing wrong with giving it an object.
If you can't do those, you'll have to call DoSomething using reflection, and you'll take a big performance hit.
First we need to convert p.Value to the right type, since even if we know the type at compile time we can't pass the string straight to the method...
DoSomething<Int32>( "10" ); // Build error
For simple numeric types and DateTime, we can use
object convertedValue = Convert.ChangeType(p.Value, p.DisplayType);
Now we can use reflection to invoke the required generic method...
typeof(ClassExample)
.GetMethod("DoSomething")
.MakeGenericMethod(p.DisplayType)
.Invoke(this, new object[] { p.Name, convertedValue });
Strictly saying you can use MethodInfo.MakeGenericMethod for this.
But I recommend to change DoSomething to non-generic form instead, since it is not evident whether it really should be generic.

Get the type from a nullable variable

public class MyType
{
public int? MyId { get; set; }
}
MyType myType = new MyType();
myType.MyId.GetType()
the last line returns an exception since MyId is not set (ie. it's null). How I get the type (int? or even int) in this case? Note, int? is used as an example, the variable may have any type, this is just a simplified example.
Note, according to Microsoft, this is supposed to work:
int? a = 17;
Type typeOfA = a.GetType();
Console.WriteLine(typeOfA.FullName);
// Output:
// System.Int32
and it does work when the value is assigned...
EDIT.
Looking at some of the replies and comments, I would like to add that in code, I pass myType.MyId as an object to a method that needs to figure out its type. Basically it looks similar to:
public void RunQuery(string sql, List<(string parameterName, object parameterValue)> parameters)
so myType.MyId is passed into RunQuery as parameterValue
You can use reflection to get declared type of a property (which is known at compile time):
Type t = typeof(MyType).GetProperty(nameof(MyType.MyId)).PropertyType;
And GetType() is used to figure out the actual type of an object in runtime, but that does not make sense for a null reference.
Edit:
When you cast Nullable<T> to an Object, its value is boxed, so, if it was null, you will get just an Object variable with null reference, and you won't be able to find out the type any more.
So, you should somehow change your infrastructure to make the type be passed with your parameter. The fastest workaround is to pass it explicitly
List<(string parameterName, object parameterValue, Type parameterType)> parameters
Check out System.Data.SqlClient.SqlParameter, I am not sure, but this is probably exactly what you need to use.
[Edited], I've re-written my solution to address your question: Making a function that is agnostic to MyType:
string GetPropertyName<T>(T prop)
{
var type = typeof(T);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return type.GenericTypeArguments[0].FullName;
}
else
{
return type.FullName;
}
}
You can now call it like this:
MyType myType = new MyType();
string name = GetPropertyName(myType.MyId);
Console.WriteLine(name); // Outputs System.Int32
I've test it, and it's working for me.
BTW, if you pass it a non-nullable member, it will return that property name. You can test that for yourself.

C# variable type name [duplicate]

I'm looking for how to get compile time type of a variable for debugging purposes.
The testing environment can be reproduced as simply as:
object x = "this is actually a string";
Console.WriteLine(x.GetType());
Which will output System.String. How could I get the compile time type System.Object here?
I took a look over at System.Reflection, but got lost in the amount of possibilities it provides.
I don't know if there is a built in way to do it but the following generic method would do the trick:
void Main()
{
object x = "this is actually a string";
Console.WriteLine(GetCompileTimeType(x));
}
public Type GetCompileTimeType<T>(T inputObject)
{
return typeof(T);
}
This method will return the type System.Object since generic types are all worked out at compile time.
Just to add I'm assuming that you are aware that typeof(object) would give you the compile time type of object if you needed it to just be hardcoded at compile time. typeof will not allow you to pass in a variable to get its type though.
This method can also be implemented as an extension method in order to be used similarly to the object.GetType method:
public static class MiscExtensions
{
public static Type GetCompileTimeType<T>(this T dummy)
{ return typeof(T); }
}
void Main()
{
object x = "this is actually a string";
Console.WriteLine(x.GetType()); //System.String
Console.WriteLine(x.GetCompileTimeType()); //System.Object
}

Getting a value from a generic method without supplying the type

I was trying to create a class were one of the properties was generic without the class itself being generic. I discover that you can't do that; generic properties aren't allowed. A bit of digging here too me to the thread Making a generic property were I found a work around that works nicely for me.
Subsequently, I ended up with this class...
[Serializable]
public class ConfigurationItem
{
private readonly Type type;
public string Description { get; set; }
public IDictionary<string, ConfigurationItem> Items { get; private set; }
public string Name { get; set; }
public string Summary { get; set; }
public object Value { get; private set; }
public ConfigurationItem(string name, Type type = null, object value = null)
{
this.Name = name;
this.type = type ?? typeof(string);
this.Value = value;
Items = new Dictionary<string, ConfigurationItem>();
}
public string Export()
{
return JsonConvert.SerializeObject(this);
}
public T GetValue<T>()
{
return (T)Convert.ChangeType(Value, type);
}
}
Now the only issue I have is that if I want to get the value, properly cast, I have to supply the type when I call GetValue(). Instinctively, I can't help but feel, given that the class knows the type that should be returned, it should be possible for me to construct a GetValue() method that doesn't require any additional information from me.
I can't figure out how.
I did find the thread Getting a generic method to infer the type parameter from the runtime type which appears to suggest that it is possible, but I understand very little about Reflection and couldn't make any sense of what was being said.
Is there a way of constructing a GetType() method that doesn't require that I supply the generic type? And can you explain it in a way that my feeble brain can comprehend it?
EDIT
A number of people have actually pointed out that really I don't need to do this anyway, nonetheless as a learning exercise I followed up the suggestion from #ShoaibShakeel to look at the C# dynamic type and came up with these additional methods...
public dynamic GetValue()
{
return typeof(ConfigurationItem)
.GetMethod("GetReturnValue", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.MakeGenericMethod(type)
.Invoke(this, new object[] { });
}
private T GetReturnValue<T>()
{
return (T)Convert.ChangeType(Value, type);
}
Instinctively, I can't help but feel, given that the class knows the type that should be returned
While it is true that the Value object knows its own type, this information is only available at runtime though (once there is an actual object that has a type). So in order to have type information at compile time, you need to supply the necessary information. After all, it’s impossible to know statically what object is being assigned there.
But assuming that GetValue() was able to return the typed object automatically, what would you want to do with it?
var x = configurationItem.GetValue();
So what type should x be of, and what would you want to do with it afterwards? Do you only want to print it or something? Then object is enough. Do you want to calculate something with it? Then you would already require that it’s an int, or a float or something, so you would need an actual static type—which is why you would have to supply that type information.
Just because the return type is object that does not mean that the object itself loses the type information. A string assigned to an object property will still be a string, even if you retrieve it much later. And if you expect a string and want to do something with it, then you can surely just cast it to a string.
No.
The compiler cannot infer the type from the variable it is assigned to since it is ambiguous, especially in large inheritance trees.
Consider the line
object o = configurationItem.GetValue<int>();
int may be a valid conversion for your value but object isn't, since objectdoes not implement the IConvertible interface (required by Convert.ChangeType().
I was able to get my class to return a value, cast correctly, using reflection and dynamics using the following additions to my original class.
public dynamic GetValue()
{
return typeof(ConfigurationItem)
.GetMethod("GetReturnValue", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.MakeGenericMethod(type)
.Invoke(this, new object[] { });
}
private T GetReturnValue<T>()
{
return (T)Convert.ChangeType(Value, type);
}

Generics in c# & accessing the static members of T

My question concerns c# and how to access Static members ... Well I don't really know how to explain it (which kind of is bad for a question isn't it?) I will just give you some sample code:
Class test<T>{
int method1(Obj Parameter1){
//in here I want to do something which I would explain as
T.TryParse(Parameter1);
//my problem is that it does not work ... I get an error.
//just to explain: if I declare test<int> (with type Integer)
//I want my sample code to call int.TryParse(). If it were String
//it should have been String.TryParse()
}
}
So thank you guys for your answers (By the way the question is: how would I solve this problem without getting an error). This probably quite an easy question for you!
Edit: Thank you all for your answers!
Though I think the try - catch phrase is the most elegant, I know from my experience with vb that it can really be a bummer. I used it once and it took about 30 minutes to run a program, which later on only took 2 minutes to compute just because I avoided try - catch.
This is why I chose the switch statement as the best answer. It makes the code more complicated but on the other hand I imagine it to be relatively fast and relatively easy to read. (Though I still think there should be a more elegant way ... maybe in the next language I learn)
Though if you have some other suggestion I am still waiting (and willing to participate)
The problem is that TryParse isn't defined on an interface or base class anywhere, so you can't make an assumption that the type passed into your class will have that function. Unless you can contrain T in some way, you'll run into this a lot.
Constraints on Type Parameters
Short answer, you can't.
Long answer, you can cheat:
public class Example
{
internal static class Support
{
private delegate bool GenericParser<T>(string s, out T o);
private static Dictionary<Type, object> parsers =
MakeStandardParsers();
private static Dictionary<Type, object> MakeStandardParsers()
{
Dictionary<Type, object> d = new Dictionary<Type, object>();
// You need to add an entry for every type you want to cope with.
d[typeof(int)] = new GenericParser<int>(int.TryParse);
d[typeof(long)] = new GenericParser<long>(long.TryParse);
d[typeof(float)] = new GenericParser<float>(float.TryParse);
return d;
}
public static bool TryParse<T>(string s, out T result)
{
return ((GenericParser<T>)parsers[typeof(T)])(s, out result);
}
}
public class Test<T>
{
public static T method1(string s)
{
T value;
bool success = Support.TryParse(s, out value);
return value;
}
}
public static void Main()
{
Console.WriteLine(Test<int>.method1("23"));
Console.WriteLine(Test<float>.method1("23.4"));
Console.WriteLine(Test<long>.method1("99999999999999"));
Console.ReadLine();
}
}
I made a static dictionary holding a delegate for the TryParse method of every type I might want to use. I then wrote a generic method to look up the dictionary and pass on the call to the appropriate delegate. Since every delegate has a different type, I just store them as object references and cast them back to the appropriate generic type when I retrieve them. Note that for the sake of a simple example I have omitted error checking, such as to check whether we have an entry in the dictionary for the given type.
To access a member of a specific class or interface you need to use the Where keyword and specify the interface or base class that has the method.
In the above instance TryParse does not come from an interface or base class, so what you are trying to do above is not possible. Best just use Convert.ChangeType and a try/catch statement.
class test<T>
{
T Method(object P)
{
try {
return (T)Convert.ChangeType(P, typeof(T));
} catch(Exception e) {
return null;
}
}
}
One more way to do it, this time some reflection in the mix:
static class Parser
{
public static bool TryParse<TType>( string str, out TType x )
{
// Get the type on that TryParse shall be called
Type objType = typeof( TType );
// Enumerate the methods of TType
foreach( MethodInfo mi in objType.GetMethods() )
{
if( mi.Name == "TryParse" )
{
// We found a TryParse method, check for the 2-parameter-signature
ParameterInfo[] pi = mi.GetParameters();
if( pi.Length == 2 ) // Find TryParse( String, TType )
{
// Build a parameter list for the call
object[] paramList = new object[2] { str, default( TType ) };
// Invoke the static method
object ret = objType.InvokeMember( "TryParse", BindingFlags.InvokeMethod, null, null, paramList );
// Get the output value from the parameter list
x = (TType)paramList[1];
return (bool)ret;
}
}
}
// Maybe we should throw an exception here, because we were unable to find the TryParse
// method; this is not just a unable-to-parse error.
x = default( TType );
return false;
}
}
The next step would be trying to implement
public static TRet CallStaticMethod<TRet>( object obj, string methodName, params object[] args );
With full parameter type matching etc.
This isn't really a solution, but in certain scenarios it could be a good alternative: We can pass an additional delegate to the generic method.
To clarify what I mean, let's use an example. Let's say we have some generic factory method, that should create an instance of T, and we want it to then call another method, for notification or additional initialization.
Consider the following simple class:
public class Example
{
// ...
public static void PostInitCallback(Example example)
{
// Do something with the object...
}
}
And the following static method:
public static T CreateAndInit<T>() where T : new()
{
var t = new T();
// Some initialization code...
return t;
}
So right now we would have to do:
var example = CreateAndInit<Example>();
Example.PostInitCallback(example);
However, we could change our method to take an additional delegate:
public delegate void PostInitCallback<T>(T t);
public static T CreateAndInit<T>(PostInitCallback<T> callback) where T : new()
{
var t = new T();
// Some initialization code...
callback(t);
return t;
}
And now we can change the call to:
var example = CreateAndInit<Example>(Example.PostInitCallback);
Obviously this is only useful in very specific scenarios. But this is the cleanest solution in the sense that we get compile time safety, there is no "hacking" involved, and the code is dead simple.
Do you mean to do something like this:
Class test<T>
{
T method1(object Parameter1){
if( Parameter1 is T )
{
T value = (T) Parameter1;
//do something with value
return value;
}
else
{
//Parameter1 is not a T
return default(T); //or throw exception
}
}
}
Unfortunately you can't check for the TryParse pattern as it is static - which unfortunately means that it isn't particularly well suited to generics.
The only way to do exactly what you're looking for would be to use reflection to check if the method exists for T.
Another option is to ensure that the object you send in is a convertible object by restraining the type to IConvertible (all primitive types implement IConvertible). This would allow you to convert your parameter to the given type very flexibly.
Class test<T>
{
int method1(IConvertible Parameter1){
IFormatProvider provider = System.Globalization.CultureInfo.CurrentCulture.GetFormat(typeof(T));
T temp = Parameter1.ToType(typeof(T), provider);
}
}
You could also do a variation on this by using an 'object' type instead like you had originally.
Class test<T>
{
int method1(object Parameter1){
if(Parameter1 is IConvertible) {
IFormatProvider provider = System.Globalization.CultureInfo.CurrentCulture.GetFormat(typeof(T));
T temp = Parameter1.ToType(typeof(T), provider);
} else {
// Do something else
}
}
}
Ok guys: Thanks for all the fish. Now with your answers and my research (especially the article on limiting generic types to primitives) I will present you my solution.
Class a<T>{
private void checkWetherTypeIsOK()
{
if (T is int || T is float //|| ... any other types you want to be allowed){
return true;
}
else {
throw new exception();
}
}
public static a(){
ccheckWetherTypeIsOK();
}
}
You probably cant do it.
First of all if it should be possible you would need a tighter bound on T so the typechecker could be sure that all possible substitutions for T actually had a static method called TryParse.
You may want to read my previous post on limiting generic types to primitives. This may give you some pointers in limiting the type that can be passed to the generic (since TypeParse is obviously only available to a set number of primitives ( string.TryParse obviously being the exception, which doesn't make sense).
Once you have more of a handle on the type, you can then work on trying to parse it. You may need a bit of an ugly switch in there (to call the correct TryParse ) but I think you can achieve the desired functionality.
If you need me to explain any of the above further, then please ask :)
Best code: restrict T to ValueType this way:
class test1<T> where T: struct
A "struct" here means a value type.
String is a class, not a value type.
int, float, Enums are all value types.
btw the compiler does not accept to call static methods or access static members on 'type parameters' like in the following example which will not compile :(
class MyStatic { public static int MyValue=0; }
class Test<T> where T: MyStatic
{
public void TheTest() { T.MyValue++; }
}
=> Error 1 'T' is a 'type parameter', which is not valid in the given context
SL.
That is not how statics work. You have to think of statics as sort of in a Global class even if they are are spread across a whole bunch of types. My recommendation is to make it a property inside the T instance that can access the necessary static method.
Also T is an actual instance of something, and just like any other instance you are not able to access the statics for that type, through the instantiated value. Here is an example of what to do:
class a {
static StaticMethod1 ()
virtual Method1 ()
}
class b : a {
override Method1 () return StaticMethod1()
}
class c : a {
override Method1 () return "XYZ"
}
class generic<T>
where T : a {
void DoSomething () T.Method1()
}

Categories