如何确定一个属性是否是一种数组。例子:publicboolIsPropertyAnArray(PropertyInfoproperty){//returntrueiftypeisIList,IEnumerable,ObservableCollection,etc...} 最佳答案 您似乎在问两个不同的问题:类型是数组(例如string[])还是任何集合类型。对于前者,只需检查property.PropertyType.IsArray.对于后者,您必须决定您希望类型符合的最低标准是什么。例如,您可以检查非通用IEnumerable通过
如何确定一个属性是否是一种数组。例子:publicboolIsPropertyAnArray(PropertyInfoproperty){//returntrueiftypeisIList,IEnumerable,ObservableCollection,etc...} 最佳答案 您似乎在问两个不同的问题:类型是数组(例如string[])还是任何集合类型。对于前者,只需检查property.PropertyType.IsArray.对于后者,您必须决定您希望类型符合的最低标准是什么。例如,您可以检查非通用IEnumerable通过
当我在其中时,我想通过反射获取属性名称。可能吗?我有这样的代码:publicCarTypeCar{get{return(Wheel)this["Wheel"];}set{this["Wheel"]=value;}}因为我需要更多这样的属性,所以我想做这样的事情:publicCarTypeCar{get{return(Wheel)this[GetThisPropertyName()];}set{this[GetThisPropertyName()]=value;}} 最佳答案 由于属性实际上只是方法,您可以执行此操作并清理返回的get
当我在其中时,我想通过反射获取属性名称。可能吗?我有这样的代码:publicCarTypeCar{get{return(Wheel)this["Wheel"];}set{this["Wheel"]=value;}}因为我需要更多这样的属性,所以我想做这样的事情:publicCarTypeCar{get{return(Wheel)this[GetThisPropertyName()];}set{this[GetThisPropertyName()]=value;}} 最佳答案 由于属性实际上只是方法,您可以执行此操作并清理返回的get
我创建了一个名为RelatedPropertyAttribute的Attribute类:[AttributeUsage(AttributeTargets.Property)]publicclassRelatedPropertyAttribute:Attribute{publicstringRelatedProperty{get;privateset;}publicRelatedPropertyAttribute(stringrelatedProperty){RelatedProperty=relatedProperty;}}我用它来表示类中的相关属性。我将如何使用它的示例:public
我创建了一个名为RelatedPropertyAttribute的Attribute类:[AttributeUsage(AttributeTargets.Property)]publicclassRelatedPropertyAttribute:Attribute{publicstringRelatedProperty{get;privateset;}publicRelatedPropertyAttribute(stringrelatedProperty){RelatedProperty=relatedProperty;}}我用它来表示类中的相关属性。我将如何使用它的示例:public
假设我有一个接口(interface)和两个类,其中一个类实现了这个接口(interface):interfaceIAAA{intF1{get;set;}}classAAA1{publicintF1{get;set;}publicintF2{get;set;}}classAAA2:IAAA{publicintF1{get;set;}publicintF2{get;set;}}在AAA2类中,属性F1是从接口(interface)IAAA“继承”(我不确定),然后我使用反射来检查一个属性是否是虚拟的:Console.WriteLine("AAA1whichdoesnotimplement
假设我有一个接口(interface)和两个类,其中一个类实现了这个接口(interface):interfaceIAAA{intF1{get;set;}}classAAA1{publicintF1{get;set;}publicintF2{get;set;}}classAAA2:IAAA{publicintF1{get;set;}publicintF2{get;set;}}在AAA2类中,属性F1是从接口(interface)IAAA“继承”(我不确定),然后我使用反射来检查一个属性是否是虚拟的:Console.WriteLine("AAA1whichdoesnotimplement
在C#中,有人可以这样做:MyClassmyInstance=newMyClass();dynamicmydynamicInstance=myInstance;然后,调用一个方法,例如://ThismethodtakesaMyClassargumentanddoessomething.Caller.InvokeMethod(myDynamicInstance);现在,这将导致在运行时确定myInstance类型,如果有效,Caller.InvokeMethod会正常调用。现在,我的问题是这是否被认为是使用dynamic的不好做法。,尤其是在以下情况下:1)InvokeMethod实例化
在C#中,有人可以这样做:MyClassmyInstance=newMyClass();dynamicmydynamicInstance=myInstance;然后,调用一个方法,例如://ThismethodtakesaMyClassargumentanddoessomething.Caller.InvokeMethod(myDynamicInstance);现在,这将导致在运行时确定myInstance类型,如果有效,Caller.InvokeMethod会正常调用。现在,我的问题是这是否被认为是使用dynamic的不好做法。,尤其是在以下情况下:1)InvokeMethod实例化