5

我有一本字典:

Dictionary<int, Type> AllDrillTypes = new Dictionary<int, Type>()
{
  {13,typeof(TCHEMISTRY)},
  {14,typeof(TDRILLSPAN)}
};

其中 TCHEMISTRY 和 TDRILLSPAN 是类。然后我想从其中一个类中获取行,如下所示:

Type T = AllDrillTypes[13];
var LC = Activator.CreateInstance( typeof(List<>).MakeGenericType( T ) );
MethodInfo M = T.GetMethod("FindAll", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);    
LC = M.Invoke(null, new object[] { });

所有这些代码都可以正常工作。之后我需要得到一些这样的行:

var LingLC = from obj in LC where obj.RunID == 1001 select obj;

但是这条线会导致错误:

“找不到源类型'object'的查询模式的实现。找不到'Where'。”

这段代码行有什么问题?

4

4 回答 4

3

即使您不能更改类定义,也可以避免使用反射:

// Getter dictionary rather than type dictionary.
Dictionary<int, Func<IEnumerable<object>>> DrillTypeGetters =
    new Dictionary<int, Func<IEnumerable<object>>>()   
    {  
        { 13, () => TCHEMISTRY.FindAll().Cast<object>() },
        { 14, () => TDRILLSPAN.FindAll().Cast<object>() }
    };
Dictionary<int, Func<object, int>> IDGetters =
    new Dictionary<int, Func<object, int>>()
    {
        { 13, o => ((TCHEMISTRY)o).RunID },
        { 14, o => ((TDRILLSPAN)o).RunID }
    };

IEnumerable<object> LC = DrillTypeGetters[13]();
IEnumerable<object> LingLC = 
    from obj in LC
    where IDGetters[13](obj) == 1001
    select obj;

或者您甚至可以打开13/并为每种类型14运行完全不同的方法。

if (choice == 13)
    IEnumerable<TCHEMISTRY> LingLC =
        TCHEMISTRY.FindAll().Where(tc => tc.RunID == 1001);
else if (choice == 14)
    IEnumerable<TDRILLSPAN> LingLC =
        TDRILLSPAN.FindAll().Where(td => td.RunID == 1001);

基本上,如果这两个类不共享任何共同的层次结构,您就不能编写任何共同的代码来处理它们。如果它们有很多相似的属性,您可以像我的第一个示例中那样使用 getter 来提供一种方法来获取您正在处理的任何类型的类的相似属性。如果它们甚至没有相似的属性,请不要尝试编写共享代码。

于 2012-10-17T08:41:35.753 回答
1

也许你可以将你的代码重写成这样的东西,....以获得更安全的解决方案(不使用反射)。

void Main()
{
    var driller1 = new DrillerWhichYouCannotChange1();
    var driller2 = new DrillerWhichYouCannotChange2();

    var allDrillTypes = new Dictionary<int, IList<IDriller>>()
    {
        { 13, new List<IDriller>() { new DrillerWhichYouCannotChange1Adapter(driller1) } },
        { 14, new List<IDriller>() { new DrillerWhichYouCannotChange2Adapter(driller2) } },
    };

    Console.WriteLine(allDrillTypes[13][0].SomeCommonProperty); // prints 123
    Console.WriteLine(allDrillTypes[14][0].SomeCommonProperty); // prints 456
}

interface IDriller
{
    int SomeCommonProperty { get; }
}

class DrillerWhichYouCannotChange1Adapter : IDriller
{
    private DrillerWhichYouCannotChange1 inner;

    public DrillerWhichYouCannotChange1Adapter(DrillerWhichYouCannotChange1 inner)
    {
        this.inner = inner;
    }

    public int SomeCommonProperty { get { return this.inner.PropertyX; } }
}

class DrillerWhichYouCannotChange2Adapter : IDriller
{
    private DrillerWhichYouCannotChange2 inner;

    public DrillerWhichYouCannotChange2Adapter(DrillerWhichYouCannotChange2 inner)
    {
        this.inner = inner;
    }

    public int SomeCommonProperty { get { return this.inner.PropertyY; } }
}

class DrillerWhichYouCannotChange1
{
    public int PropertyX { get { return 123; } }
}

class DrillerWhichYouCannotChange2
{
    public int PropertyY { get { return 456; } }
}

编辑:如果您无法更改司钻类,您可以使用适配器模式为每个司钻创建一个适配器,实现IDiller.

于 2012-10-17T08:37:34.527 回答
0

尝试

IEnumerable returnedObjects = (IEnumerable)M.Invoke(null, new object[] { }) as IEnumerable;

然后遍历你的 ienumerable

foreach (object report in returnedObjects)
{
    // Use reflection to read properties or add to a new List<object> if you
    // need an ICollection<object>
}

代替 :

LC = M.Invoke(null, new object[] { });
于 2012-10-17T08:50:45.540 回答
0

您需要将 LC 转换为 FindAll 方法的返回类型。类似的东西:

var genericList = ((List<TChemistry>) LC);
var LingLC = from obj in genericList where obj.RunID == 1001 select obj;

这是假设 FindAll 返回 TChemistry 的集合。

- 编辑

如果您在运行时不知道类型是 TChemistry 还是 TDrillspan,则必须编写一个 if/else 的 switch 语句来转换为正确的类型。我宁愿让 TChemistry 和 TDrillSpan 扩展一个抽象类或接口,您可以只转换为 List,并且您将始终拥有 RunId 属性。

public abstract class TAbstract
{
   public abstract int RunId {get; set;}
}

public class TChemistry : TAbstract
{
    public override int RunId {get; set;}
}

public class TDrillSpan : TAbstract
{
   public override int RunId {get; set;}
}

Type T = AllDrillTypes[13] as TAbstract;
var LC = Activator.CreateInstance( typeof(List<>).MakeGenericType( T ) );
MethodInfo M = T.GetMethod("FindAll", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);    
LC = M.Invoke(null, new object[] { });

var genericList = ((List<TAbstract>) LC);
var LingLC = from obj in genericList where obj.RunID == 1001 select obj;

如果你不能改变类的声明,那么你只剩下丑陋的 if else:

var typeInfo = LC.GetType();
IEnumerable<T> genericList;
if (typeInfo == typeof(IEnumerable<TChemistry>)
{
    genericList = (List<TChemistry>) LC;
)
else if (typeInfo == typeof(IEnumerable<TDrillSpan>)
{
    genericList = (List<TDrillSpan>) LC;
}

var LingLC = from obj in genericList where obj.RunID == 1001 select obj;
于 2012-10-17T08:31:04.953 回答