Cast object to method generic type - c#

This generates an error saying I cannot convert type ClassType
to T. Is there any workaround for this?
Is there any way to specify that the type of this can in fact be converted to T?
public void WorkWith<T>(Action<T> method)
{
method.Invoke((T)this);
}

Two possible solutions:
Not type-safe:
public void WorkWith<T>(Action<T> method)
{
method.Invoke((T)(object)this);
}
This isn't typesafe because you can pass it any method that has a single parameter and no return value, like:
WorkWith((string x) => Console.WriteLine(x));
The typesafe "version" (using generic constraints):
public class MyClass
{
public void WorkWith<T>(Action<T> method) where T : MyClass
{
method.Invoke((T)this);
}
}
The point here is that to be able to cast this to T, the compiler wants to be sure that this is always castable to T (so the need for the constraint). As shown in the not-type-safe example, the "classical" (unsafe) solution used with generics is passing through a cast to object.

public void WorkWith<T>(Action<T> method) where T: ClassType {
method.Invoke((T)this);
}

Related

Casting T from and to Generic<T> (C#)

So I have a generic class, with Type as its generic parameter. In this class is a method, which has object parameter called value. Kinda like this:
public class Foo<Type> where Type : IComparable
{
public void Bar(object value)
{
DoSomething((Type)value);
}
}
As you can notice, I need to "DoSomething" with value (stored in object) that I first need to cast to Type. I even have my own overridden cast, which works on its own.
In this specific case, Type is generic, lets call it GenericType, and has this user-defined cast:
public static implicit operator GenericType<T>(T value)
{
return new GenericType<T>(value);
}
and value is an enum, lets say
public enum Number: short
{
Zero = 0,
One = 1,
Two = 2
}
The 'DoSomething((Type)value)' in this case is where Type is GenericType and value is Number.Zero. For some reason, this causes the cast to throw InvalidCastException: Specified cast is not valid. When I try it directly, i mean like..
GenericType<Number> z = (GenericType<Number>)Number.Zero;
..it works (I know, there is not explicit cast even needed). But for some reason, it does not work in the complex example I stated above. Can anyone help me understand and potentially fix that?
Why not just let your class use the generic type?
public class Foo<T> where T : IComparable
{
public void Bar(T value)
{
DoSomething(value);
}
}
No casting needed... and please don't use reserved words to name stuff.

Cast needed for generic constraints

I have created a method with two generic parameters where one parameter (itemsToAdd) must be the same type as the generic parameter of the next parameter (inputList).
See this demo code:
public class GenericsDemo
{
public void AddToList<TList, TItems>(TList inputList, params TItems[] itemsToAdd)
where TItems : IConvertible
where TList : IEnumerable<TItems>
{
IEnumerable<IConvertible> someOtherList;
// Sounds good, doesn't work..
//someOtherList = inputList;
// This works
someOtherList = (IEnumerable<IConvertible>)inputList;
}
}
I would expect the inputList can be directly assigned into the IEnumerable<IConvertible> someOtherList, but it needs a cast. Why the cast is needed?
Covariance only works for classes, not for structs (Source).
Thus, if you restrict TItems to reference types, your code compiles (fiddle):
where TItems : class, IConvertible

Cannot pass variable of type conforming to generic constraint

public interface ILovable<T> where T : IEquatable<T>
{
T Care(T t);
}
public class Me : ILovable<int>
{
public int Care(int i)
{
return i;
}
}
Say I have the above. Now below function fails:
private static void Colour<T>(ILovable<T> me) where T : IEquatable<T>
{
var z = me.Care(1); //cannot convert from 'int' to 'T'
}
What's failing the above piece of code? ILovable<T> has a Care function which intakes a T which is IEquatable<T>. In the above function I'm calling the same Care function and passing T which is int type. int is after all IEquatable<int>.
What am I doing wrong? Is there any work around to get it fixed?
Your method signature does not specify a ILovable<int>, it specifies an ILovable<T>. This, for example, would work:
private static void Colour(ILovable<int> me)
{
var z = me.Care(1); //cannot convert from 'int' to 'T'
}
The problem is the compiler doesn't know that T is an 'int' in your example; it could be any type that meets the constraint. Here is another way that would work:
private static void Colour<T>(ILovable<T> me, T valueToCareAbout) where T : IEquatable<T>
{
var z = me.Care(valueToCareAbout);
}
//use like this
Colour(me, 1);
The error I get is:
Argument type 'int' is not assignable to parameter type 'T'
I'm pretty sure this is because you are defining me as an ILovable<T>. Therefore, it doesn't automatically resolve to the Me type where int is defined as T.
This will fix the error because Me defines T as an int:
private static void Colour<T>(Me me) where T : IEquatable<T>
{
var z = me.Care(1);
}
Well that's because method Colour says that there will be parameter of type ILovable< T > whereas T would be resolved later, so at compile time either I tell method that T is int type.
So either you pass ILovable as parameter and grantee that T is int
void Colour<T>(ILovable<int> me)
or pass type Me directly
void Colour<T>(Me me)
Because otherwise me.Care is expecting type T not int as specific
Change following
private static void Colour<T>(ILovable<T> me) where T : IEquatable<T>
To
private static void Colour<Int32>(ILovable<int> me)
and above will work.
Now the mystry portion
You are getting error in following
private static void Colour<T>(ILovable<T> me) where T : IEquatable<T>
because Care is expecting T, and you are providing int.
It is same as
Care((T)1).
or
T t = (T)1; //This is the cause of error as int cannot be changed to T. Remember Int32 is sealed so T cannot derive from int
Care(t); // This is fine
To make above work, T has to int. To make it so, Colur method syntax should be like
private static void Colour<Int32>(ILovable<int> me)
If you want to pass string to Care, T should be string.
private static void Colour<string>(ILovable<string> me)
{
me.Care("Hello");
}
Now if we have to fix T then question arises why T is required at all in Colour definition.
Answer -> For non taking care of inheritance type in non sealed class.
The short answer is overriding variable of type T inside a generic method (or class) with a more derived type is not possible since compiler doesn't explicitly know T is that more derived type (in our case T is int), because T can be any other more derived type at run time.
Long answer: me variable is of type ILovable<T>. Now me.Care function is expecting parameter of type that is specified on ILovable<T> which is T. Outside the Care function T can be anything that is IEquatable<T>, so int is ok. But inside the function, T has to be just T and not another derived type of IEquatable<T>. Otherwise there will be runtime error for scenarios like this:
private static void Colour<T>(ILovable<T> me) where T : IEquatable<T>
{
var z = me.Care(1);
}
...
Colour("");
Right now, T is string when calling Colour(""). So me is ILovable<string>. So me.Care function expects a string as parameter but provided is an int and that is disaster.

Casting one generic to another, without class constraints

I have the following code:
public interface IDrilldown
{
void AddCriteria<T>(T Criterion);
}
public class MyClass<W> : IDrilldown // where W : class
{
void IDrilldown.AddCriteria<T>(T Criterion)
{
W value = Criterion as W;
...
}
}
Unfortunately, the cast I have above will not work unless W has the constaint in the code. I would like to have this using value types. Is it at all possible?
I cannot make W and T the same type. My interface does not have a type associated with it globally, only the internal data types.
This is so that I can have a List all having different T's
I was able to find a way to do it, it's a little hacky but allows it to work:
class MyClass<W> : IDrilldown {
void IDrilldown.AddCriteria<T>(T Criterion) {
if (Criterion is W) {
W value = (W)Convert.ChangeType(Criterion, typeof(W));
// value is W, have fun
// or - as Snowbear pointed out in the comments
W value = (W)(object)Criterion;
// works just as well....
} else {
// value is NOT W and could not be converted.
}
}
}
The only drawback with this is, Convert.ChangeType will use converters to change between internal objects, so string value = (string)Convert.ChangeType(1, typeof(string)) will work and return "1" instead of throwing an exception.
To clarify on how this works, the documentation states:
For the conversion to succeed, value must implement the IConvertible interface, because the method simply wraps a call to an appropriate IConvertible method. The method requires that conversion of value to conversionType be supported.
so for this method to work with custom types you will need to implement the IConvertible interface to convert from one custom type to any other type. In the code sample above, if both T and W are the same type, the Convert.ChangeType will succeed, even if the custom object does not implement IConvertiable.
Would the dynamic keyword help you out?
Something like this:
public interface IDrilldown
{
void AddCriteria<T>(T Criterion);
}
public class MyClass : IDrilldown
{
void IDrilldown.AddCriteria<T>(T criterion)
{
dynamic value = criterion;
// can use typeof() to figure out type if needed...
...
}
}

C# generic interface type inference question

I'm not sure how to phrase this question concisely without just giving the example so here goes:
public interface IThing<T>
{
void Do(T obj);
}
public class ThingOne : IThing<int>
{
public void Do(int obj)
{
}
}
public class ThingTwo : IThing<string>
{
public void Do(string obj)
{
}
}
public class ThingFactory
{
public IThing<T> Create<T>(string param)
{
if (param.Equals("one"))
return (IThing<T>)new ThingOne();
if (param.Equals("two"))
return (IThing<T>)new ThingTwo();
}
}
class Program
{
static void Main(string[] args)
{
var f = new ThingFactory();
// any way we can get the compiler to infer IThing<int> ?
var thing = f.Create("one");
}
}
The question appears to be here:
// any way we can get the compiler to infer IThing<int> ?
var thing = f.Create("one");
No. You would need to explicitly specify the type:
var thing = f.Create<int>("one");
You can't infer the return type without having a parameter used specifically in the method. The compiler uses the parameters passed to the method to infer the type T, and in this case, it's a single string parameter, with no parameters of type T. As such, there's no way to have this inferred for you.
No, you can't do this because the result of your Create factory method will be evaluated at runtime based on the value of the parameter. Generics are for compile-time safety and in your case you cannot have such safety because the parameter value will be known only at runtime.

Categories