I have the following types defined:
-----------------------------------
interface IMyInterface<T> where T : class
{
void BlahBlah();
}
abstract class MyBase<T> : IMyInterface<T> where T : class
{
public abstract T MyInfo { get; set; }
public abstract T MyInfo2 { get; set; }
public void BlahBlah()
{
Console.WriteLine(MyInfo + ", " + MyInfo2);
}
}
class MyConcreteClass1<T> : MyBase<T> where T : class
{
public override T MyInfo { get; set; }
public override T MyInfo2 { get; set; }
}
class MyConcreteClass2<T> : MyBase<T> where T : class
{
public override T MyInfo { get; set; }
public override T MyInfo2 { get; set; }
}
public static class DataOperations<T, U> where T : class where U : class
{
public static U Operate(this T source, U destination)
{
var sourceProps = source.GetType().GetProperties();
var destinationProps = destination.GetType().GetProperties();
.
.
.
return destination;
}
}
我想要做的是在实现中对上述两个类进行操作:
MyConcreteClass1<String> class1 = new MyConcreteClass1<String>();
class1.MyInfo = "Some Value";
class1.MyInfo2 = "another value";
MyConcreteClass2<String> class2 = new MyConcreteClass2<String>();
var c2 = class1.Operate(class2);
问题是我得到:DataOperations类名上的“必须在非通用静态类中定义CS1106扩展方法”。
声明类型必须是非泛型的,但是方法可以是泛型的。请考虑:
public static class DataOperations
{
public static U Operate<T, U>(this T source, U destination)
where T : class where U : class
{ // ...
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句