2012-06-19 82 views
1

我想弄清楚如何可以使一个泛型调用采取变量的类型。在下面的调用中,输入“DAL.Account”类型,并且工作正常。物业类型作为通用参数

var tst = ctx.GetTable<DAL.Account>().Where(t => t.Sbank == "000134"); 

我想改变这种状况,让我来代替“DAL.Account”的传递变量。像这样的东西,但我知道这将无法正常工作,因为您无法将属性作为类型传递。

ctx.GetTable<Criteria.EntityType>().Where(LinqToSQLHelper.BuildWhereStatement(Criteria.StateBag), Criteria.StateBag.Values.ToArray()) 

下面是我认为解释我正在尝试做什么的shell代码段。泛型不是我的强项,所以我正在寻求一些帮助。无论如何,我可以做到这一点?

//Stores a "Type" that indicates what Object is a Criteria for. 
public class AccountCriteria : IGeneratedCriteria 
{ 
    ... 

    public Type EntityType 
    { 
     get {return typeof(DAL.Account);} 
    } 
} 

//I have added a function to the DataContext called "GetTable" 
// And then used it as an example in a Console App to test its functionality. 
public class ADRPDataContext : NHibernateDataContext 
{ 
    ... 

    public CodeSmith.Data.NHibernate.ITable<T> GetTable<T>() where T : EntityBase 
    { 
     var tb = new CodeSmith.Data.NHibernate.Table<T>(this); 
     return tb; 
    } 
} 

// console application that uses DataContext.GetTable 
class Program 
{ 
    static void Main(string[] args) 
    { 
     using (var ctx = new ADRPDataContext()) 
     { 
      var tst = ctx.GetTable<DAL.Account>().Where(t => t.Sbank == "000134"); 
     } 
    } 
} 

//ExistsCommand class that uses the EntityType property of the Critera to generate the data. 
public class ExistsCommand 
{ 
    private IGeneratedCriteria Criteria { get; set; } 

    protected override void DataPortal_Execute() 
    { 
     using (var ctx = new DC.ADRPDataContext()) 
     { 
      //This was my first attempt but doesn't work becuase you can't pass a property in for a Type. 
      //But I can figure out how to write this so that it will work. 
      Result = ctx.GetTable<Criteria.EntityType>().Where(LinqToSQLHelper.BuildWhereStatement(Criteria.StateBag), Criteria.StateBag.Values.ToArray()).Count() > 0; 
     } 
    } 
} 

回答

2

您正在寻找实例化一个泛型类型。一些信息可以发现here

这是一个简单的例子演示了如何实例化一个列表与3这里的容量是可以调用创建一个通用的,当你不知道该类型的方法:

public static Object CreateGenericListOfType(Type typeGenericWillBe) 
    { 
     //alternative to the followin: 
     //List<String> myList = new List<String>(3); 


     //build parameters for the generic's constructor (obviously this code wouldn't work if you had different constructors for each potential type) 
     object[] constructorArgs = new Object[1]; 
     constructorArgs[0] = 3; 


     //instantiate the generic. Same as calling the one line example (commented out) above. Results in a List<String> with 3 list items 
     Type genericListType = typeof(List<>); 
     Type[] typeArgs = { typeGenericWillBe }; 
     Type myNewGeneric = genericListType.MakeGenericType(typeArgs); 
     object GenericOfType = Activator.CreateInstance(myNewGeneric, constructorArgs); 


     return GenericOfType; 
    } 

这里是一些示例代码,将显示您的实例方法的工作原理:

List<String> Strings = (List<String>)InstantiateGenericTypeWithReflection.CreateGenericListOfType(typeof(String)); 

     //demonstrate the object is actually a List<String> and we can do stuff like use linq extensions (isn't a good use of linq but serves as example) 
     Strings.Add("frist"); 
     Strings.Add("2nd"); 
     Strings.Add("tird"); 
     Console.WriteLine("item index 2 value: " + Strings.Where(strings => strings == "2").First()); 

在你的榜样,你的GetTable<Criteria.EntityType>()CreateGenericTableOfType(Criteria.EntityType)取代。这将返回一个通用的任何类型的通用表。您当然需要正确实现该方法(处理构造函数参数,将List更改为Table等)。

0

我认为你需要改变你稍微做这件事的方式,而不是使用泛型而不是EntityType属性。也许沿着以下几点:

// Create an abstract class to be used as the base for classes that are supported by 
// ExistsCommand and any other classes where you need a similar pattern 
public abstract class ExtendedCriteria<T> : IGeneratedCriteria 
{ 
    public ExistsCommand GetExistsCommand() 
    { 
     return new ExistsCommand<T>(this); 
    } 
} 

// Make the non-generic ExistsCommand abstract 
public abstract class ExistsCommand 
{ 
    protected abstract void DataPortal_Execute(); 
} 

// Create a generic sub-class of ExistsCommand with the type parameter used in the GetTable call 
// where you were previously trying to use the EntityType property 
public class ExistsCommand<T> : ExistsCommand 
{ 
    protected override void DataPortal_Execute() 
    { 
     using (var ctx = new DC.ADRPDataContext()) 
     { 
      Result = ctx.GetTable<T>().Where(LinqToSQLHelper.BuildWhereStatement(Criteria.StateBag), Criteria.StateBag.Values.ToArray()).Count() > 0; 
     } 
    } 

} 

// Derive the AccountCriteria from ExtendedCriteria<T> with T the entity type 
public class AccountCriteria : ExtendedCriteria<DAL.Account> 
{ 
    ... 
}