PropertyCopy 一般将字符串值从 Null 更改为 Empty

发布于 2024-10-28 04:12:26 字数 1973 浏览 1 评论 0原文

我遇到了一个问题,即我控制之外的代码类使用空字符串,因此当它们被引用时,例如“string.Length”,会导致错误。我想也许我可以创建一些更简单的东西,而不是使用嵌套类编写平均可能的 100 个字段的检查。我有一个想法...

如果您对复制对象进行过任何研究 PropertyCopy 以及其他一些是非常常见的发现。我目前使用上面提到的类。我想知道是否可以将其修改为简单地: 如果 stringPropertyValue 为 null,则将 stringPropertyValue 设置为等于 string.Empty。

我的理解是有限的。我一直在进行研究来解决我的问题,但没有真正的好主意。我的想法可以实现吗?有更好的办法吗?如果可以的话会怎样做呢?

更新

根据下面的回复,我创建了当前要使用的此类。

public static void DenullifyStringsToEmpty<T>(this T instance)
    {
        //handle properties
        foreach (var filteredProperties in instance.GetType().GetProperties().Where(p =>
            (p.PropertyType.IsClass || p.PropertyType.IsInterface || p.PropertyType == typeof(string))))
        {
            if (filteredProperties.PropertyType == typeof(string))
            {
                if (filteredProperties.GetValue(instance, null) == null)
                {
                    filteredProperties.SetValue(instance, string.Empty, null);
                }
            }
            else
            {
                filteredProperties.GetValue(instance, null).DenullifyStringsToEmpty();
            }
        }

        //handle fields
        foreach (var filteredFields in instance.GetType().GetFields().Where(f =>
            (f.FieldType.IsClass || f.FieldType.IsInterface || f.FieldType == typeof(string))))
        {
            if (filteredFields.FieldType == typeof(string))
            {
                if (filteredFields.GetValue(instance) == null)
                {
                    filteredFields.SetValue(instance, string.Empty);
                }
            }
            else
            {
                filteredFields.GetValue(instance).DenullifyStringsToEmpty();
            }
        }
    }

我知道反思可能很繁重,在我们遇到问题之前,我认为这个解决方案会非常有效。这是一个扩展(感谢下面的评论)。

感谢您的意见。

I've ran into an issue where code classes outside of my control use strings that are null so when they become referenced for example, "string.Length", causes an error. Rather than write a check for the possible 100 fields on average, with nested classes, I thought maybe I could create something easier. I had an idea...

If you've done any research into copying objects PropertyCopy, along with a few others, is an extremely common find. I currently use the class mentioned above. I was wondering if it could be modified to simply go:
if stringPropertyValue is null then set stringPropertyValue equal to string.Empty.

My understanding is limited. I've been doing research to solve my issue but no real good ideas. Can my idea work? Is there a better way? How would it be done if it could?

Update:

Based on a response below I have created this class which I am currently going to use.

public static void DenullifyStringsToEmpty<T>(this T instance)
    {
        //handle properties
        foreach (var filteredProperties in instance.GetType().GetProperties().Where(p =>
            (p.PropertyType.IsClass || p.PropertyType.IsInterface || p.PropertyType == typeof(string))))
        {
            if (filteredProperties.PropertyType == typeof(string))
            {
                if (filteredProperties.GetValue(instance, null) == null)
                {
                    filteredProperties.SetValue(instance, string.Empty, null);
                }
            }
            else
            {
                filteredProperties.GetValue(instance, null).DenullifyStringsToEmpty();
            }
        }

        //handle fields
        foreach (var filteredFields in instance.GetType().GetFields().Where(f =>
            (f.FieldType.IsClass || f.FieldType.IsInterface || f.FieldType == typeof(string))))
        {
            if (filteredFields.FieldType == typeof(string))
            {
                if (filteredFields.GetValue(instance) == null)
                {
                    filteredFields.SetValue(instance, string.Empty);
                }
            }
            else
            {
                filteredFields.GetValue(instance).DenullifyStringsToEmpty();
            }
        }
    }

I know that reflection can be heavy and until we have an issue I think this solution will work great. This is an extension (thanks to the comments below).

Thanks for the input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2024-11-04 04:12:26

难道你不能创建一个简单的扩展方法吗?

public static string NullToEmpty(this string possibleNullString)
{
    return possibleNullString ?? string.Empty;
}

在访问第三方类的字符串属性时使用此属性,例如:

var length = instanceOfThirdPartyClass.StringProperty.NullToEmpty().Length;

更新:
现在我明白你想要什么了;-)
看看这个:

public static void DenullStringProperties<T>(this T instance)
{
    foreach(var propertyInfo in instance.GetType().GetProperties().
                                   Where(p => p.PropertyType == typeof(string))
    {
        var value = propertyInfo.GetValue(instance, null);
        if(value == null)
            value = string.Empty;
        propertyInfo.SetValue(instance, value, null);
    }
}

你可以这样称呼它:

instanceOfThirdPartyClass.DenullStringProperties();

但我仍然认为你应该采用第一种方法,因为我真的没有理由在运行时执行如此繁重的工作(反射并不便宜),只是因为您在开发过程中懒得打字:)此外,您无法确定在调用 DenullStringProperties (多线程、调用对象的方法、. ..)。第一种方法检查 null 并在需要时处理它。

Couldn't you just create a simple extension method?

public static string NullToEmpty(this string possibleNullString)
{
    return possibleNullString ?? string.Empty;
}

Use this, when accessing the string properties of that third party classes, e.g.:

var length = instanceOfThirdPartyClass.StringProperty.NullToEmpty().Length;

Update:
Now that I understand what you want ;-)
Have a look at this:

public static void DenullStringProperties<T>(this T instance)
{
    foreach(var propertyInfo in instance.GetType().GetProperties().
                                   Where(p => p.PropertyType == typeof(string))
    {
        var value = propertyInfo.GetValue(instance, null);
        if(value == null)
            value = string.Empty;
        propertyInfo.SetValue(instance, value, null);
    }
}

You could call it like this:

instanceOfThirdPartyClass.DenullStringProperties();

But I still think you should go with the first approach, because I really don't see a reason to do such heavy lifting during runtime (reflection isn't cheap), just because you are lazy about typing during development :) Additionally, you can't be sure that the properties will stay non null after you have called DenullStringProperties (multi-threading, calls to methods of the object, ...). The first approach checks for null and handles it just as it is needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文