Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 减少重复结构的属性_C#_Properties - Fatal编程技术网

C# 减少重复结构的属性

C# 减少重复结构的属性,c#,properties,C#,Properties,我有一个类,它具有许多表示客户的自动字符串属性。简而言之,是这样的: public class Person { public string FirstName { get; set; } public string LastName { get; set; } } 这些数据来自客户端和使用Nuget包的许多不同文件导入。这些客户机似乎是从数据库生成的系统中复制数据,我们有时会在某些字段中获得文本“NULL”字符串。我想以一种简单的方式去掉这些代码,并尽可能地对现有代

我有一个类,它具有许多表示客户的自动字符串属性。简而言之,是这样的:

public class Person 
{    
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
这些数据来自客户端和使用Nuget包的许多不同文件导入。这些客户机似乎是从数据库生成的系统中复制数据,我们有时会在某些字段中获得文本“NULL”字符串。我想以一种简单的方式去掉这些代码,并尽可能地对现有代码进行一些更改。我不希望抛出错误,我只希望它为null而不是“null”(不是Address2的验证失败,而是LastName的验证失败)

因为有很多导入,而且我使用的是一个没有源代码的Nuget库,所以我无法真正捕获传入的数据。在我们处理它之前,所有这些都会在相当少的类中结束,所以这似乎是一个进行处理的地方。我在这里列出了两个字段,但有十几个字段,而且不仅仅是一个类。我们也经常使用ValueInjector,所以将字符串替换为自定义类实际上不是一个选项

我能想到的最好办法是撤消自动属性,并使用字符串扩展方法进行空检查。那就像是一大堆重复的浮肿。有没有办法在一个属性上定义这个操作,并说每次都这样做,而不需要将一行代码转换成十三行几十次

public static class StringExtensions
{
    public static string NullMeansNull(this string value)
    {
        if (value == "NULL")
            return null;
        else
            return value;
    }
}

public class Person 
{
    private string _FirstName;
    public string FirstName
    {
        get
        {
            return this._FirstName.NullMeansNull();
        }

        set
        {
            this._FirstName = value;
        }
    }

    private string _LastName;
    public string LastName
    {
        get
        {
            return this._LastName.NullMeansNull();
        }

        set
        {
            this._LastName = value;
        }
    }
}

注意,我不是在谈论代码格式。我们有足够的StyleCop和CodeAnalysism规则,可以像上面那样进行扩展,这对于您真正想要阅读的代码来说是很好的。

我放弃了不接触每个导入的尝试。我只是在导入查找任何可编辑字符串属性的数据、检查“NULL”或空字符串并将其转换为NULL后调用了一个通用扩展方法

public static T CleanNullishStrings<T>(this T obj) where T : class
{
    foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties())
    {
        if (property.PropertyType == typeof(string) && property.CanRead && property.CanWrite)
        {
            string value = (string)property.GetMethod.Invoke(obj, null);
            value = string.IsNullOrEmpty(value) || value.ToUpper() == "NULL" ? null : value;
            property.SetMethod.Invoke(obj, new object[] { value });
        }
    }

    return obj;
}
publicstatict-cleanullishstrings(这个T-obj),其中T:class
{
foreach(obj.GetType().GetProperties()中的System.Reflection.PropertyInfo属性)
{
if(property.PropertyType==typeof(string)&&property.CanRead&&property.CanWrite)
{
string value=(string)property.GetMethod.Invoke(obj,null);
value=string.IsNullOrEmpty(value)| | value.ToUpper()=“NULL”?NULL:value;
调用(obj,新对象[]{value});
}
}
返回obj;
}
因为它通常返回传递的对象,所以我可以在投影中使用它:

IEnumerable<Person> = personList.Select(p => p.CleanNullishStrings());
IEnumerable=personList.Select(p=>p.cleanullishstrings());

可能会帮助您(隐式运算符-使类充当字符串)这很有趣。我以前从未见过隐式运算符。不幸的是,ValueInjector对类型非常挑剔,只有当属性是完全相同的类型时才复制它们。它甚至不会将Guid复制到可为null的属性中。