我有一个扩展方法将类转换为datatable,在此扩展方法参数中,我可以指定在转换时我不希望在数据表中添加的类的属性。
这就是我调用扩展方法的方法。
class Program
{
static void Main(string[] args)
{
CancelMaintenance cm = new CancelMaintenance ();
cm.UserID = 1;
cm.CancelReasonCodeID = 2;
cm.CancelRefund = "C";
cm.Description = "test";
cm.ReasonCode = "TestCode";
DataTable dt = cm.ConvertToDataTable2(x => x.Test1, x => x.Test2);
}
}
我在这里调用的扩展方法就像这样
cm.ConvertToDataTable2(x => x.Test1, x => x.Test2);
我想这样打电话
cm.ConvertToDataTable2(x => x.Test1, x.Test2);
那么如何更改我的扩展方法Expression
类型参数以接受我期望的属性。
扩展方法
public static class MyExt{
public static DataTable ConvertToDataTable<T>(this T instance, params Expression<Func<T, object>>[] proprtiesToSkip) where T : EntityBase
{
//Sample code ...
DataTable dataTable = new DataTable();
return dataTable;
}
}
样本类
public class EntityBase
{
public int UserID{ get; set; }
}
public class CancelMaintenance : EntityBase
{
public int CancelReasonCodeID { get; set; }
public string ReasonCode { get; set; }
public string Description { get; set; }
public string CancelRefund { get; set; }
public string Test1 { get; set; }
public string Test2 { get; set; }
}