C# C语言中的多级反射#
我正在尝试编写一个函数,它可以接收URI,然后将URI映射到C#中的函数。其目的是在C#中有一个对象,该对象具有许多可以调用的函数或成员。这类似于CherryPy中的方法调度器 我在这方面做了很多工作,但在通过多层反射时,我一直在用C#抓住物体。目前,我有一个函数,它将通过成员对象的几个层递归,但这是使用类型完成的,而不是直接对象 我发现这很难解释,所以希望下面的代码能够澄清这个问题(我已经删除了所有错误检查和out子句) 我遇到的问题是,在调用MethodInfo.Invoke时,我没有物理对象引用作为第一个参数传入 我相信我应该能够以某种方式从“fieldInfo”获取对象引用,如果有人能告诉我如何做,我相信我可以完成此功能C# C语言中的多级反射#,c#,web,reflection,C#,Web,Reflection,我正在尝试编写一个函数,它可以接收URI,然后将URI映射到C#中的函数。其目的是在C#中有一个对象,该对象具有许多可以调用的函数或成员。这类似于CherryPy中的方法调度器 我在这方面做了很多工作,但在通过多层反射时,我一直在用C#抓住物体。目前,我有一个函数,它将通过成员对象的几个层递归,但这是使用类型完成的,而不是直接对象 我发现这很难解释,所以希望下面的代码能够澄清这个问题(我已经删除了所有错误检查和out子句) 我遇到的问题是,在调用MethodInfo.Invoke时,我没有物理对
{
List<String> path = request.Url.AbsolutePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList<String>();
object[] parameters = new object[2] { request, response };
System.Type controllerType = Controller.GetType();
MethodInfo methodInfo = GetMethod(path, controllerType);
methodInfo.Invoke(<I DONT HAVE THIS OBJECT>, parameters);
}
private MethodInfo GetMethod(List<String> path, System.Type type)
{
MethodInfo methodInfo = type.GetMethod(path[0], BindingFlags.Public | BindingFlags.Instance);
if(methodInfo != null)
{
return methodInfo;
}
FieldInfo fieldInfo = type.GetField(path[0].ToString());
path.RemoveAt(0);
return GetMethod(path, fieldInfo.FieldType);
}
{
List path=request.Url.AbsolutePath.Split(新字符[]{'/},StringSplitOptions.RemoveEmptyEntries.ToList();
object[]parameters=新对象[2]{request,response};
System.Type controllerType=Controller.GetType();
MethodInfo MethodInfo=GetMethod(路径,控制器类型);
调用(,参数);
}
私有MethodInfo GetMethod(列表路径,System.Type)
{
MethodInfo MethodInfo=type.GetMethod(路径[0],BindingFlags.Public | BindingFlags.Instance);
如果(methodInfo!=null)
{
返回方法信息;
}
FieldInfo FieldInfo=type.GetField(路径[0].ToString());
移除路径(0);
返回GetMethod(路径、fieldInfo.FieldType);
}
无法从中获取对象引用。包括字段信息在内的类型信息是元数据。如果您创建了100个T
类型的实例,那么共享类型信息应该引用哪个实例
但是,您声明“目的是在C#中拥有一个对象,该对象具有多个可调用的函数或成员。”因此,这听起来可能适合您:
public static class SingletonHelper
{
public static T GetInstance<T>() where T : class
{
return Singleton<T>.Instance;
}
public static object GetInstance(Type type)
{
if (type == null)
throw new ArgumentNullException();
if (!type.IsClass)
throw new InvalidOperationException();
var genericType = typeof(Singleton<>).MakeGenericType(new Type[] { type });
return genericType.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public).GetValue(null, null);
}
}
public class Singleton<T> where T : class
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
/// <summary>
/// Private nested class which acts as singleton class instantiator. This class should not be accessible outside <see cref="Singleton<T>"/>
/// </summary>
class Nested
{
/// <summary>
/// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
/// </summary>
static Nested()
{
}
/// <summary>
/// Static instance variable
/// </summary>
internal static readonly T instance = (T)Activator.CreateInstance(typeof(T), true);
}
public static T Instance { get { return Nested.instance; } }
}
公共静态类SingletonHelper
{
公共静态T GetInstance(),其中T:class
{
返回Singleton.Instance;
}
公共静态对象GetInstance(类型)
{
if(type==null)
抛出新ArgumentNullException();
如果(!type.IsClass)
抛出新的InvalidOperationException();
var genericType=typeof(Singleton).MakeGenericType(新类型[]{Type});
返回genericType.GetProperty(“实例”,BindingFlags.Static | BindingFlags.Public).GetValue(null,null);
}
}
公共类单例,其中T:class
{
//显式静态构造函数告诉C#编译器
//不将类型标记为beforefieldinit
静态单态()
{
}
///
///充当单例类实例化器的私有嵌套类。该类不应在外部访问
///
类嵌套
{
///
///显式静态构造函数告诉C#编译器不要将类型标记为beforefieldinit
///
静态嵌套()
{
}
///
///静态实例变量
///
内部静态只读T实例=(T)Activator.CreateInstance(typeof(T),true);
}
公共静态T实例{get{return Nested.Instance;}}
}
您可以在这里调用
SingletonHelper.GetInstance(type)
。这要求您的代码只能使用一个。如果没有,这就行不通。您考虑过只使用WebAPI吗?它已经这样做了,像这样吗?