简体   繁体   English

系统结构/类的自动可为空的转换操作符

[英]Automatic Nullable to non nullable conversion operator for system structs/classes

I am attempting to convert a huge project away from using XSD's to using entities, but a problem with this is that XSD's did not generate Nullable types, they generated non-nullable types and generated methods such as IsMyFieldNameNull() . 我试图将一个庞大的项目从使用XSD转换为使用实体,但这是一个问题,即XSD不会生成Nullable类型,它们会生成非Nullable类型,并会生成IsMyFieldNameNull()

I would like to have our entities work properly and be nullable types, but i would not like to go round the (400,000 SLOC) project and add .Value to all instance of every access to an entity property. 我想让我们的实体正常工作并且是可以为null的类型,但是我不想遍历(400,000 SLOC)项目并将.Value添加到对实体属性的每次访问的所有实例中。

I came up with: 我想出了:

namespace System
{
    public partial struct Int64
    {
        public static explicit operator Int64(Int64? d)
        {
            return (d.Value);
        }
    }
}

But am being given the error "Conversion operator with same parameter and return types". 但是出现错误“具有相同参数和返回类型的转换运算符”。 Does anybody know how to best work around this, and if the code above is even possible? 有没有人知道如何最好地解决此问题,甚至上面的代码甚至可能?

To resolve this, maintain backwards compatibility with existing code, good practice with future code. 要解决此问题,请保持与现有代码的向后兼容性,以及对未来代码的良好实践。 I came up with the following code: 我想出了以下代码:

public class Auto<T> where T: struct
{
    private T? _value;

    public Auto()
    {
        _value = default(T);
    }

    public Auto(T val)
    {
        _value = val;
    }

    public Auto(T? val)
    {
        _value = val;
    }

    public bool HasValue
    {
        get
        {
            return _value.HasValue;
        }
    }

    public T Value
    {
        get { return _value.HasValue ? _value.Value : default(T); }
    }

    public static implicit operator T(Auto<T> d)
    {
        return d._value.HasValue ? d._value.Value : default(T);
    }

    public static implicit operator Auto<T>(T d)
    {
        return new Auto<T>(d);
    }

    public static implicit operator T?(Auto<T> d)
    {
        return (d._value);
    }

    public static implicit operator Auto<T>(T? d)
    {
        return new Auto<T>(d);
    }
}

To be used: 要使用的:

class MyEntity
{
    public Auto<long> ID;
    public Auto<short> Status;
    public String Name;

   //rest of entity code here
}

This way, i do not have to go around the existing code base adding .HasValue 's and .Values everywhere, and i also dont have to use IsStatusNull() (though these methods are required for compatibility reasons, they will simply return HasValue ). 这样,我不必.HasValue现有代码库,而在各处都添加.HasValue.Values ,并且我也不必使用IsStatusNull() (尽管出于兼容性考虑,这些方法是必需的,但它们只会返回HasValue )。 。

Not sure how helpful this would be to others, but this is the most sensible way of creating my new DAL layer to replace my old bad DAL layer, and having to change as little as possible in the existing BLL and App layers. 不确定这对其他人有多大帮助,但这是创建我的新DAL层来替换旧的不良DAL层,并且必须在现有的BLL和App层中进行尽可能小的更改的最明智的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM