草庐IT

Reflection

全部标签

c# - 接口(interface)上的属性

我有一个接口(interface),它定义了一些带有属性的方法。这些属性需要从调用方法中访问,但我拥有的方法不会从接口(interface)中提取属性。我错过了什么?publicclassSomeClass:ISomeInterface{MyAttributeGetAttribute(){StackTracestackTrace=newStackTrace();StackFramestackFrame=stackTrace.GetFrame(1);MethodBasemethodBase=stackFrame.GetMethod();object[]attributes=methodB

c# - 接口(interface)上的属性

我有一个接口(interface),它定义了一些带有属性的方法。这些属性需要从调用方法中访问,但我拥有的方法不会从接口(interface)中提取属性。我错过了什么?publicclassSomeClass:ISomeInterface{MyAttributeGetAttribute(){StackTracestackTrace=newStackTrace();StackFramestackFrame=stackTrace.GetFrame(1);MethodBasemethodBase=stackFrame.GetMethod();object[]attributes=methodB

c# - 反射 - 检查所有可为空的属性是否有值

我必须遍历几个类中的所有属性并检查任何可为null的属性以查看它们是否具有值。如何将从propertyInfo.GetValue()返回的值转换为通用可为null的类型,以便检查HasValue属性?为简洁起见,代码被剪掉了:foreach(PropertyInfopropInfointhis.GetType().GetProperties()){if(){//Howdoicastthisproperlyinheretoallowmetodo:if(!((Nullable)propInfo.GetValue(this,null)).HasValue)//Morecodehere}}

c# - 反射 - 检查所有可为空的属性是否有值

我必须遍历几个类中的所有属性并检查任何可为null的属性以查看它们是否具有值。如何将从propertyInfo.GetValue()返回的值转换为通用可为null的类型,以便检查HasValue属性?为简洁起见,代码被剪掉了:foreach(PropertyInfopropInfointhis.GetType().GetProperties()){if(){//Howdoicastthisproperlyinheretoallowmetodo:if(!((Nullable)propInfo.GetValue(this,null)).HasValue)//Morecodehere}}

c# - 属性/方法/成员是否有等效于 typeof 的 C#?

类类型元数据可以通过多种方式获得。其中两个是:vartypeInfo=Type.GetType("MyClass")和vartypeInfo=typeof(MyClass)第二种方式的优点是拼写错误会被编译器捕获,IDE可以理解我在说什么(允许重构等功能在不破坏代码的情况下工作)是否存在为元数据和反射强引用成员/属性/方法的等效方法?我可以更换吗:varpropertyInfo=typeof(MyClass).GetProperty("MyProperty")用类似的东西:varpropertyInfo=property(MyClass.MyProperty)

c# - 属性/方法/成员是否有等效于 typeof 的 C#?

类类型元数据可以通过多种方式获得。其中两个是:vartypeInfo=Type.GetType("MyClass")和vartypeInfo=typeof(MyClass)第二种方式的优点是拼写错误会被编译器捕获,IDE可以理解我在说什么(允许重构等功能在不破坏代码的情况下工作)是否存在为元数据和反射强引用成员/属性/方法的等效方法?我可以更换吗:varpropertyInfo=typeof(MyClass).GetProperty("MyProperty")用类似的东西:varpropertyInfo=property(MyClass.MyProperty)

c# - 如何获取基类的泛型类型参数?

我有如下三个类:publicclassTestEntity{}publicclassBaseClass{}publicclassDerivedClass:BaseClass{}我已经在运行时使用反射获得了DerivedClass的System.Type对象。如何使用反射获取TestEntity的System.Type对象?谢谢。 最佳答案 我假设您的代码只是一个示例,您并没有明确知道DerivedClass。vartype=GetSomeType();varinnerType=type.BaseType.GetGenericArgu

c# - 如何获取基类的泛型类型参数?

我有如下三个类:publicclassTestEntity{}publicclassBaseClass{}publicclassDerivedClass:BaseClass{}我已经在运行时使用反射获得了DerivedClass的System.Type对象。如何使用反射获取TestEntity的System.Type对象?谢谢。 最佳答案 我假设您的代码只是一个示例,您并没有明确知道DerivedClass。vartype=GetSomeType();varinnerType=type.BaseType.GetGenericArgu

c# - 如何在运行时加载程序集并创建类实例?

我有一个集会。在这个程序集中,我有一个类和接口(interface)。我需要在运行时加载这个程序集,并想创建一个类的对象,还想使用该接口(interface)。AssemblyMyDALL=Assembly.Load("DALL");//DALLisnameofmydllTypeMyLoadClass=MyDALL.GetType("DALL.LoadClass");//LoadClassismyclassobjectobj=Activator.CreateInstance(MyLoadClass);这是我的代码。如何改进? 最佳答案

c# - 如何在运行时加载程序集并创建类实例?

我有一个集会。在这个程序集中,我有一个类和接口(interface)。我需要在运行时加载这个程序集,并想创建一个类的对象,还想使用该接口(interface)。AssemblyMyDALL=Assembly.Load("DALL");//DALLisnameofmydllTypeMyLoadClass=MyDALL.GetType("DALL.LoadClass");//LoadClassismyclassobjectobj=Activator.CreateInstance(MyLoadClass);这是我的代码。如何改进? 最佳答案