- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想读取对象的所有公共(public)属性值并编写了以下代码:
private List<PropertyInfo> GetListOfProperties(object objectToRegister,
BindingFlags bindingFlags = BindingFlags.Instance |
BindingFlags.Public)
{
Type type = objectToRegister.GetType();
List<PropertyInfo> curListOfProperties = new List<PropertyInfo>();
curListOfProperties.AddRange(type.GetProperties()
.Where((propertyInfo) =>
!propertyInfo.GetIndexParameters().Any()));
return curListOfProperties;
}
然后这样调用它:
var objectToRegister = new MemoryStream();
// ... eventually write things into MemoryStream e.g. Image.Save(objectToRegister , "Bmp")
// ... eventually do nothing with objectToRegister
foreach (var propertyInfo in GetListOfProperties(objectToRegister))
{
if (propertyInfo.CanRead)
{
// -->> TargetInvocationException
value = propertyInfo.GetValue(objectToRegister , null);
}
}
异常看起来像这样
System.InvalidOperationException: Timeouts are not supported on this stream. at System.IO.Stream.get_ReadTimeout()
现在我想从 GetListOfProperties
的返回值中排除这些不受支持的属性
最佳答案
我觉得你的代码本身没问题。
但我怀疑您的方法存在一个基本的设计缺陷:即您假设您可以随时随地以任何顺序成功读取任何属性。
精心设计的类型可能会满足这个假设,但不幸的是,有些类型遵循不同的协议(protocol):
一个对象可能有一个属性 HasValue
,它指定是否可以查询另一个属性 Value
(或者这是否会导致 InvalidOperationException
或类似的)。
(设计更好的类型可能有 TryGetValue
方法或可为空的 Value
属性。)
一个对象可能必须先被初始化
-d,然后才能对其进行任何操作。
等您在 Stream.ReadTimeout
中遇到了另一个这样的例子,显然 MemoryStream
不支持它。
如果你必须让你的反射代码与任何类型一起工作,这里有一些选项:
最简单的方法是通过将对 propertyInfo.GetValue
的调用包装在 try
/catch
中来简单地“忽略”任何错误> block (并可能将所有捕获的异常收集到 AggregateException
中)。
如果您想以不同的方式对待某些特定类型以解决特定问题(例如您使用 MemoryStream
的情况),您可以创建反射代码的各种实现并根据以下内容选择策略对象的 Type
。这是一个非常粗略的例子,可以给你一个想法:
interface IPropertyInspector
{
PropertyInfo[] GetProperties(object obj);
}
class GenericPropertyInspector : IPropertyInspector { /* your current implementation */ }
class StreamPropertyInspector : IPropertyInspector { /* does not return e.g. ReadTimeout if CanTimeout is false */ }
Dictionary<Type, IPropertyInspector> inspectors = ...;
inspectors[typeof(MemoryStream)] = new StreamPropertyInspector();
...
Type t = objectToRegister.GetType();
IPropertyInspector inspector;
if (!inspectors.TryGetValue(t, out inspector))
{
inspector = new GenericPropertyInspector();
}
var properties = inspector.GetProperties(objectToRegister):
// do something with properties
这种额外的间接级别将允许您过滤掉已知会导致问题的属性。
关于c# - TargetInvocationException @ PropertyInfo.GetValue(target, null) 与目标类型 MemoryStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33066378/
我正在制作一个辅助方法,它会自动为给定实体(类)的属性设置随机值,这样我就不必在测试时为每个属性填充一个值。 在我的例子中,每个实体都继承自 BaseEntity 类,该类具有 ID、CreatedB
我知道 typeof(T) == typeof(T)永远为真,因为 Type对象基本上是静态的,只有一个 Type每个类类型都存在实例(如果这是错误的,请纠正我......我有几个程序在这个假设下运行
我有以下类属性: [EffectAspect(Enums.Effects.Low)] public int Wind { set; get; } [EffectAspect(Enums.Effects
例如,我有这个简单的类: public class MyClass { public String MyProperty { get; set; } } 获取 MyProperty 的 Prope
假设我有这门课: class Test123 where T : struct { public Nullable Test {get;set;} } 和这个类 class Test321 {
如何按照类中的排列顺序获取类型 FieldInfos/PropertyInfos 作为 MemberInfo 数组? class Test { public bool First { get;
我想获取特定属性的 PropertyInfo。我可以使用: foreach(PropertyInfo p in typeof(MyObject).GetProperties()) { if (
我想忽略属性信息如下的属性; PropertyInfo propertyInfo = typeof(GLAccount).GetProperty("ExampleProp"); modelBuilde
我验证了 GetHashCode 和 Equals 允许将 PropertyInfo 用作字典键。 (具体来说,哈希码相同,Equals 返回 true。) 那么使用 PropertyInfo 作为字
我对 C# 反射有疑问。我要反射(reflect)的对象如下: public partial class ApplicationUser : IdentityUser { public App
我在反射、动态调用对象和读取集合值方面遇到问题。 在 Referenced COM/Interop 中,它看起来像这样: ICollection collection = (ICollection)s
这个问题在这里已经有了答案: PropertyInfo : is the property an indexer? (2 个答案) 关闭 7 年前。 我在一个类中有一个索引器方法,允许我这样做: v
这个问题在这里已经有了答案: Using PropertyInfo.GetValue() (1 个回答) 关闭 5 年前。 我有以下类(class): public class MagicMetad
我需要通过 Tag 类在 BFrame 类中设置 Value 属性。 我应该如何设置 Value 属性(property)? 澄清: 我不是要设置 Frame 的值 内的属性 Tag 类但是 Valu
使用反射我只想检索具有 get 和 set 方法的属性,而忽略只有 get 的属性.我想做的是为用户提供他/她能够更改的变量列表,因此向他们显示只有 get 方法的属性会产生误导。 给定下面的代码,用
这个问题在这里已经有了答案: How to get the PropertyInfo of a specific property? (5 个答案) 关闭 9 年前。 我在 Myclass 中有一个
我正在创建一个小型验证框架,我有一个可分配给方法的自定义 Validation Attribute 和 ValidationCore 中的一个 IsValid 属性> 类。当 IsValid 在方法内
在第一个内部循环中,当从 PropertyInfo[] 获取值时,我能够传递正确的目标对象,但是在第二个内部循环中,它给出了目标对象不正确的异常. 所以我想要的是获取具有此 listPropertie
我有一个带有子类地址的客户类 internal class Customer { public int id { get; set; } public string name {
我遇到了与 this one 类似的错误,但不幸的是不是同样简单的解决方案。这是代码: public virtual void MapObject(T obj, IViewModel viewMode
我是一名优秀的程序员,十分优秀!