创建一个 class 来调用实体 C#
Creating a class to call entities C#
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 调用的数百个方法中的两个示例。这是我的目标。我想创建一个 class 允许一个人传入方法和参数的名称并返回指定的任何 return 类型,而不必一遍又一遍地编写上述类型的代码.有什么建议么??
您可以尝试使用反射。查看下面的 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);
}
}
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 调用的数百个方法中的两个示例。这是我的目标。我想创建一个 class 允许一个人传入方法和参数的名称并返回指定的任何 return 类型,而不必一遍又一遍地编写上述类型的代码.有什么建议么??
您可以尝试使用反射。查看下面的 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);
}
}