- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
下面是我用来获取类中所有公共(public)属性的初始状态以进行 IsDirty 检查的一些代码。
查看属性是否为 IEnumerable 的最简单方法是什么?
干杯,
贝瑞尔
protected virtual Dictionary<string, object> _GetPropertyValues()
{
return _getPublicPropertiesWithSetters()
.ToDictionary(pi => pi.Name, pi => pi.GetValue(this, null));
}
private IEnumerable<PropertyInfo> _getPublicPropertiesWithSetters()
{
return GetType().GetProperties().Where(pi => pi.CanWrite);
}
我最后做的是添加一些库扩展,如下所示
public static bool IsNonStringEnumerable(this PropertyInfo pi) {
return pi != null && pi.PropertyType.IsNonStringEnumerable();
}
public static bool IsNonStringEnumerable(this object instance) {
return instance != null && instance.GetType().IsNonStringEnumerable();
}
public static bool IsNonStringEnumerable(this Type type) {
if (type == null || type == typeof(string))
return false;
return typeof(IEnumerable).IsAssignableFrom(type);
}
最佳答案
if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string))
关于c# - 如何知道 PropertyInfo 是否是一个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3569811/
我正在制作一个辅助方法,它会自动为给定实体(类)的属性设置随机值,这样我就不必在测试时为每个属性填充一个值。 在我的例子中,每个实体都继承自 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
我是一名优秀的程序员,十分优秀!