C# 获取具有反射的字段

C# 获取具有反射的字段,c#,class,reflection,C#,Class,Reflection,我想获取所有具有空值的字段,但我甚至没有获取任何字段: [Serializable()] public class BaseClass { [OnDeserialized()] internal void OnDeserializedMethod(StreamingContext context) { FixNullString(this); } public void FixNullString(object type) {

我想获取所有具有空值的字段,但我甚至没有获取任何字段:

  [Serializable()]
public class BaseClass
{
    [OnDeserialized()]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        FixNullString(this);
    }

    public void FixNullString(object type)
    {
        try
        {
            var properties = type.GetType().GetFields();


            foreach (var property in from property in properties
                                     let oldValue = property.GetValue(type)
                                     where oldValue == null
                                     select property)
            {
                property.SetValue(type, GetDefaultValue(property));
            }
        }
        catch (Exception)
        {

        }
    }

    public object GetDefaultValue(System.Reflection.FieldInfo value)
    {
        try
        {
            if (value.FieldType == typeof(string))
                return "";

            if (value.FieldType == typeof(bool))
                return false;

            if (value.FieldType == typeof(int))
                return 0;

            if (value.FieldType == typeof(decimal))
                return 0;

            if (value.FieldType == typeof(DateTime))
                return new DateTime();
        }
        catch (Exception)
        {

        }

        return null;
    }
}
然后我有一节课:

    [Serializable()]
public class Settings : BaseClass
{
    public bool Value1 { get; set; }
    public bool Value2 { get; set; }
}
但是当我谈到

 var properties = type.GetType().GetFields();
然后我得到0个字段,它应该找到2个字段


type.getType().GetFields()使用错误吗?还是我向基类发送了错误的类?

Value1
Value2
设置中,类是属性而不是字段,因此需要使用
GetProperties()
访问它们

(使用
{get;set;}
语法告诉编译器您需要一个属性,但它应该为您生成
get
set
,以及包含数据的隐藏私有字段。)

方法返回所有公共字段。编译器为您自动生成的字段是私有的,因此您需要指定正确的
BindingFlags

type.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)

编译器生成的与类的属性对应的字段具有该属性。此外,编译器将根据属性的声明生成处理这些字段的
get
set
方法

编译器生成的属性MSDN文档:

区分编译器生成的元素和用户生成的元素。该类不能被继承

这些字段的名称的格式为
k_BackingField
,方法
set
get
名称的格式为
set_PropertyName
get_PropertyName
,其中PropertyName是属性的名称

例如,您的
设置
类编译如下:

[Serializable]
public class Settings : BaseClass
{
    public Settings(){}

    // Properties
    [CompilerGenerated]
    private bool <Value1>k__BackingField;

    [CompilerGenerated]
    private bool <Value2>k__BackingField;

    [CompilerGenerated]
    public void set_Value1(bool value)
    {
        this.<Value1>k__BackingField = value;
    }

    [CompilerGenerated]
    public bool get_Value1()
    {
        return this.<Value1>k__BackingField;
    } 

    [CompilerGenerated]
    public void set_Value2(bool value)
    {
        this.<Value2>k__BackingField = value;
    }

    [CompilerGenerated]
    public bool get_Value2()
    {
        return this.<Value2>k__BackingField;
    }
}
[可序列化]
公共类设置:基类
{
公共设置(){}
//性质
[编译生成]
私人布尔·库尤·贝林菲尔德;
[编译生成]
私人布尔·库尤·贝林菲尔德;
[编译生成]
公共无效集_值1(布尔值)
{
this.k_uubackingfield=值;
}
[编译生成]
公共图书馆获取价值1()
{
返回此.k__BackingField;
} 
[编译生成]
公共无效集_值2(布尔值)
{
this.k_uubackingfield=值;
}
[编译生成]
公共图书馆获取价值2()
{
返回此.k__BackingField;
}
}
如果要排除此支持字段,可以使用以下方法:

public IEnumerable<FieldInfo> GetFields(Type type)
{
    return type
        .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
        .Where(f => f.GetCustomAttribute<CompilerGeneratedAttribute>() == null);
}
public IEnumerable GetFields(类型)
{
返回类型
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
。其中(f=>f.GetCustomAttribute()==null);
}