Reputation: 280
I have a class:
public class ClassA<T>
{
public T Value {get; set;}
public string Description {get; set;}
}
ClassA can have a value of any type, however: I need to be able to set other properties of anything that is ClassA regardless of the type value. So I have a method:
public void DoWork(ClassA<object> source, ClassA<object> destination)
{
destination.Description = source.Description;
}
However when I try to do this I get a runtime error saying that:
cannot convert from ClassA<DateTime>
to ClassA<object>
I thought that anything could be passed as an object... I've looked all over the web for something similar, maybe I'm just not getting the keywords right.
Anyone have any suggestions?
Thanks
Upvotes: 4
Views: 171
Reputation: 203850
You won't be able to do that.
If you could pass a ClassA<int>
into a method accepting a ClassA<object> wrapper
then that method would be able to call wrapper.Value = "not an integer";
. What should happen then? You've just set a string to an object that can only hold integers. Since the compiler knows it can't enforce what you could put it, it just doesn't let you pass the object in the first place.
As for the actual solution, two come to mind.
You could make your method generic, if that's an option:
public void DoWork<TSource, TDestination>(ClassA<TSource> source, ClassA<TDestination> destination)
{
destination.Description = source.Description;
}
Or you could make a covariant interface:
public interface IWrapper<out T>
{
T Value {get;}
string Description {get;set;}
}
public class ClassA<T> : IWrapper<T>
{
//...
}
public void DoWork(IWrapper<object> source, IWrapper<object> destination)
{
destination.Description = source.Description;
}
For your DoWork
method you don't even actually use the Value
, so you could make the interface non-generic, non-covariant, and just remove Value
. The advantage here is that you could use that interface to access the value properties of any number of ClassA
objects that have a common base type.
Upvotes: 4
Reputation: 82136
If you just need to be able to use various generic types you can specify a different type for each parameter e.g.
public void Work<TSource, TDestination>(ClassA<TSource> source, ClassA<TDestination> destination)
{
destination.Description = source.Description;
}
However, it looks like your real issue here is you have common properties on a generic class, you can eliminate the generic part by using a common interface e.g.
public interface IClassA
{
string Description { get; set; }
}
public class ClassA<T> : IClassA
{
...
}
public void DoWork(IClassA source, IClassA destination)
{
destination.Description = source.Description;
}
Upvotes: 4
Reputation: 1503799
This doesn't really have anything to do with extension methods. It has to do with generic variance. It's simplest to see it like this:
ClassA<DateTime> x = new ClassA<DateTime>();
ClassA<Object> y = x;
This will fail to compile in the same way.
As of C# 4, there is some limited support for generic variance, but:
See MSDN for more details, or Eric Lippert's blog posts.
That explains why it doesn't work. As for how to fix it - making the extension method generic as per Jason's answer is probably the simplest approach.
Note that at the moment, you haven't even got an extension method - you'd need to declare it in a top-level non-generic static class, like this:
public static void DoWork<T>(this ClassA<T> source, ClassA<T> destination)
{
destination.Description = source.Description;
}
Upvotes: 2
Reputation: 12859
I assume you mean this:
public void DoWork<T>(ClassA<T> source, ClassA<T> destination)
{
destination.Description = source.Description;
}
Upvotes: 0