创建一个类来调用实体c#
本文关键字:调用 实体 一个 创建 | 更新日期: 2023-09-27 18:14:27
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调用的方法的例子。这是我的目标。我想创建一个类,允许一个人传入方法和参数的名称,并返回任何返回类型指定,而不是不得不写一遍又一遍以上类型的代码。有什么建议吗? ?
您可以尝试使用反射。查看下面的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);
}
}