Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在泛型方法中传递/指定属性?_C#_.net_Generics_Func - Fatal编程技术网

C# 在泛型方法中传递/指定属性?

C# 在泛型方法中传递/指定属性?,c#,.net,generics,func,C#,.net,Generics,Func,我正在尝试将我编写的一些代码转移到一个更通用的方法中。虽然方法较长,但我遇到的问题如下: public static void Test() { MyObjectType[] list1 = ListMyObjectTypeMethod1(); MyObjectType[] list2 = ListMyObjectTypeMethod2(); List<MyObjectType> linqAblelis

我正在尝试将我编写的一些代码转移到一个更通用的方法中。虽然方法较长,但我遇到的问题如下:

public static void Test()

{    
           MyObjectType[] list1 = ListMyObjectTypeMethod1();
            MyObjectType[] list2 = ListMyObjectTypeMethod2();

            List<MyObjectType> linqAblelist1 = new List<MyObjectType>(list1);
            List<MyObjectType> linqAblelist2 = new List<MyObjectType>(list2);

            IEnumerable<MyObjectType> toBeAdded = linqAblelist1.Where(x => linqAblelist2.All(y => y.Property1 != x.Property1));
            IEnumerable<MyObjectType> toBeDeleted = linqAblelist2.Where(a => linqAblelist1.All(b => b.Property1 != a.Property1));

}
公共静态无效测试()
{    
MyObjectType[]list1=ListMyObjectTypeMethod1();
MyObjectType[]list2=ListMyObjectTypeMethod2();
List linqAblelist1=新列表(list1);
List linqAblelist2=新列表(list2);
IEnumerable tobeaded=linqAblelist1.Where(x=>linqAblelist2.All(y=>y.Property1!=x.Property1));
IEnumerable toBeDeleted=linqAblelist2.其中(a=>linqAblelist1.All(b=>b.Property1!=a.Property1));
}
我试图为MyObjectType传入泛型类型,但是我有[如何在这里设置属性?]如何在方法的参数中指定它

public static void Test<T>(T[] x, T[] y)
        {
            List<T> list1 = new List<T>(x);
            List<T> list2 = new List<T>(y);
            IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => y.[How To Set Property Here?] != x.[How To Set Property Here?]));
            IEnumerable<T> toBeDeleted = list2.Where(a => list1.All(b => b.[How To Set Property Here?])); != a.[How To Set Property Here?]));));

        }
公共静态空隙试验(T[]x,T[]y)
{
列表1=新列表(x);
列表2=新列表(y);
IEnumerable tobeaded=list1.Where(x=>list2.All(y=>y.[如何在此处设置属性?]!=x.[如何在此处设置属性?]);
IEnumerable toBeDeleted=list2.Where(a=>list1.All(b=>b.[如何在此处设置属性?]);!=a.[如何在此处设置属性?]););
}

将选择的属性作为
Func
传递:


最好的选择是引入泛型类型约束,以确保
T
继承特定类或实现接口。无论哪种情况,类或接口都必须声明
属性1
。例如,像这样:

public static void Test<T>(T[] x, T[] y) where T : IHasProperty1
{
    …
}
公共静态空隙试验(T[]x,T[]y),其中T:IHasProperty1
{
…
}

您需要对泛型类型设置一些约束

public static void Test<T>(T[] x, T[] y) where T : <SomeInterface>
{
   List<T> list1 = new List<T>(x);
   List<T> list2 = new List<T>(y);
   IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => y.PropertyName != x.PropertyName));
    IEnumerable<T> toBeDeleted = list2.Where(a => list1.All(b => b.PropertyName)); != a.PropertyName));));

}
公共静态空隙试验(T[]x,T[]y),其中T:
{
列表1=新列表(x);
列表2=新列表(y);
IEnumerable tobeaded=list1.Where(x=>list2.All(y=>y.PropertyName!=x.PropertyName));
IEnumerable toBeDeleted=list2.Where(a=>list1.All(b=>b.PropertyName));!=a.PropertyName)););
}

您可以添加一个通用约束,以确保
T
具有您期望的属性。比如:

public static void Test<T>(T[] x, T[] y) where T : MyObjectType
{
    List<T> list1 = new List<T>(x);
    List<T> list2 = new List<T>(y);
    IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => y.Property1  != x.Property1 ));
    IEnumerable<T> toBeDeleted = list2.Where(a => list1.All(b => b.Property1 )); != a.[How To Set Property Here?]));));

}
公共静态无效测试(T[]x,T[]y),其中T:MyObjectType
{
列表1=新列表(x);
列表2=新列表(y);
IEnumerable tobeaded=list1.Where(x=>list2.All(y=>y.Property1!=x.Property1));
IEnumerable toBeDeleted=list2.Where(a=>list1.All(b=>b.Property1));!=a[如何在此处设置属性?););
}

您真的需要通用方法吗?因为您在方法中所做的操作似乎是特定于一个类型的。+1是一个简短而具体的示例。如果您可以更改底层类型,您应该只移动
property1{get;}
property2{get;}
在interface@Habib他可能是在与一个接口对抗,这将避免必须有一个接口约束+1好主意。我得到一个无法应用此代码的运算符编译器错误。确切的错误是不能在TProperty和TProperty之间应用运算符。若要将T约束为对象的实现,最好去掉泛型。
public static void Test<T>(T[] x, T[] y) where T : <SomeInterface>
{
   List<T> list1 = new List<T>(x);
   List<T> list2 = new List<T>(y);
   IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => y.PropertyName != x.PropertyName));
    IEnumerable<T> toBeDeleted = list2.Where(a => list1.All(b => b.PropertyName)); != a.PropertyName));));

}
public static void Test<T>(T[] x, T[] y) where T : MyObjectType
{
    List<T> list1 = new List<T>(x);
    List<T> list2 = new List<T>(y);
    IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => y.Property1  != x.Property1 ));
    IEnumerable<T> toBeDeleted = list2.Where(a => list1.All(b => b.Property1 )); != a.[How To Set Property Here?]));));

}