使用 C# 泛型扩展方法对 class 而不是集合进行操作
using c# generics extension methods to operate on a class and not a collection
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;
}
}
我想做的是在实现中对上述两个 classes 进行操作:
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);
问题是我得到:
"CS1106 Extension method must be defined in a non-generic static class" DataOperations class 名称。
声明类型必须是非泛型,但方法可以是泛型。改为考虑:
public static class DataOperations
{
public static U Operate<T, U>(this T source, U destination)
where T : class where U : class
{ // ...
}
}
将通用定义移动到方法上:
public static class DataOperations
{
public static U Operate<T, U>(this T source, U destination) where T : class where U : class
{
// Whatever
}
}
必须在 非通用 静态(顶级)class.
中定义扩展方法 - 作为错误状态
因此您需要将 DataOperations
class 设为非泛型,并将方法本身改为泛型:
public static class DataOperations
{
public static U Operate<T, U>(this T source, U destination) where T : class where U : class
{
var sourceProps = source.GetType().GetProperties();
var destinationProps = destination.GetType().GetProperties();
.
.
.
return destination;
}
}
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;
}
}
我想做的是在实现中对上述两个 classes 进行操作:
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);
问题是我得到: "CS1106 Extension method must be defined in a non-generic static class" DataOperations class 名称。
声明类型必须是非泛型,但方法可以是泛型。改为考虑:
public static class DataOperations
{
public static U Operate<T, U>(this T source, U destination)
where T : class where U : class
{ // ...
}
}
将通用定义移动到方法上:
public static class DataOperations
{
public static U Operate<T, U>(this T source, U destination) where T : class where U : class
{
// Whatever
}
}
必须在 非通用 静态(顶级)class.
中定义扩展方法 - 作为错误状态因此您需要将 DataOperations
class 设为非泛型,并将方法本身改为泛型:
public static class DataOperations
{
public static U Operate<T, U>(this T source, U destination) where T : class where U : class
{
var sourceProps = source.GetType().GetProperties();
var destinationProps = destination.GetType().GetProperties();
.
.
.
return destination;
}
}