public INSERT_RESULT INSERT_NEW_RECORD(string FirstName, string LastName, string MiddleIntial )
{
decimal? InsertedUID;
var nResult = new ObjectParameter("nResult", typeof(decimal));
INSERT_RESULT insertResult = new INSERT_RESULT();
try
{
using (var myDBEntities = dbEntities.GetNewEntities())
{
InsertedUID = myDBEntities.INSERT_LOOKUP(0, LastName, FirstName, MiddleIntial, dbEntities.global_USER_LOGGED_IN, nResult).FirstOrDefault();
}
insertResult.WasInserted = true;
insertResult.InsertedUID = InsertedUID ?? 0;
insertResult.ExceptionText = "NONE";
}
catch (Exception ex)
{
insertResult.WasInserted = false;
insertResult.InsertedUID = 0;
insertResult.ExceptionText = ex.InnerException.ToString();
}
return insertResult;
}
public List<GET_ITEMS_Result> GET_ITEMS(decimal itemUID)
{
var result = new List<GET_ITEMS_Result>();
using (var myDBEntities = dbEntities.GetNewEntities())
{
result = myDBEntities.GET_ITEMS(itemUID).ToList();
}
return result;
}
以上是可以从myDBEntities调用的数百种方法的示例。这是我的目标。我想创建一个类,允许一个人传入方法和参数的名称,并返回指定的返回类型,而不必反复编写上述类型的代码。有什么建议??
答案 0 :(得分:0)
您可以尝试使用反射。查看下面的InvokeMethodWithParams方法。
class Program
{
static void Main(string[] args)
{
var instance = new AClassWithHundredsOfMethods();
var result = InvokeMethodWithParams<AClassWithHundredsOfMethods>(instance, "INSERT_NEW_RECORD", "MyFirstName", "MyLastName", "MyMiddleInitial");
Console.WriteLine(result);
}
static object InvokeMethodWithParams<T>(object instance, string methodName, params object[] parameters) where T : class
{
return typeof(T).GetMethod(methodName).Invoke(instance, parameters);
}
}