【问题标题】:Method what sets the flag according to the property value根据属性值设置标志的方法
【发布时间】:2017-09-25 19:01:41
【问题描述】:

我有一个 Java Webservice,我正在.Net 中实现一个客户端。现在我遇到了类似的问题:Java Webservice and .NET client dropping DateTime objects。 当我在属性中有值时,我知道如何设置标志,但我需要一个自动设置该标志的方法。例如(我删除了属性以增加可读性):

public class MyClass
{
    private System.DateTime endDateTimeField;

    private bool endDateTimeFieldSpecified;

    private System.DateTime startDateTimeField;

    private System.DateTime firstDriverStartField;

    private System.DateTime firstDriverEndField;

    private System.DateTime secondDriverStartField;

    private bool secondDriverStartFieldSpecified;

    private System.DateTime secondDriverEndField;

    private bool secondDriverEndFieldSpecified;

    private string exciseNumberField;

    private double gasCounterEndField;

    private double gasCounterStartField;

    private string idField;

    private double mileageStartField;

    private double mileageEndField;

    private obtcStatus obtcStatusField;

    private string secondDriverField;
}

所有简单类型的属性都会自动添加“MyProperty”+指定。 我需要一个方法来检查整个对象,或者它是否具有 MyProperty / MyPropertySpecified 对,并且如果 MyProperty 设置为 null 以外的值,则将 MyPropertySpecified 标志设置为 true。

编辑: 我的问题涉及领域: 私有 System.DateTime endDateTimeField; private bool endDateTimeFieldSpecified;

私有 System.DateTime secondDriverStartField; private bool secondDriverStartFieldSpecified;

私有 System.DateTime secondDriverEndField; private bool secondDriverEndFieldSpecified;

也许我可以使用这样的东西:

public static void SetProperyValue(object obj) { IEnumerable 道具 = obj.GetType().GetProperties() .Where(x => x.Name == "endDateTimeField" || x.Name == "endDateTimeFieldSpecified");

        if (props?.Count() == 2 &&
            props.FirstOrDefault(x => x.Name == "endDateTimeField").GetValue(obj) != null)            
            props.FirstOrDefault(x => x.Name == "endDateTimeFieldSpecified").SetValue(obj, true);            
    }

但我不想在刚性上使用属性名称

【问题讨论】:

  • 我不明白如果您需要一种不知道属性名称的方法,那么您只能使用反射。
  • @Newer:你能回答我的问题吗?你知道编译时属性的名称吗?否则很难找到相关值属性的类型。因为没有Int-type(它是System.Int32)。因此,您还需要一个字典来将您的缩写名称翻译成真正的 .NET 类型。
  • @TimSchmelter 是的,我知道所有属性的名称,因为它是在 VS 中将服务引用添加到 C# 时自动生成的。在我不改变接口(WSDL)之前,Poproepty 不会改变。

标签: c# properties


【解决方案1】:

好的,我找到了解决方案:

    public static void SetPropertyValues(object obj)
    {
        IEnumerable<PropertyInfo> props = obj.GetType().GetProperties().Where(x => x.Name.EndsWith("Specified"));
        List<Tuple<string, string>> propertyList = new List<Tuple<string, string>>();

        foreach (var item in props)            
            propertyList.Add(new Tuple<string, string>(item.Name.Replace("Specified",""), item.Name));            

        foreach (var item in propertyList)           
            setPropertyValue(obj, item.Item1, item.Item2);            
    }

    private static void setPropertyValue(object obj, string propertyName, string flagName)
    {
        IEnumerable<PropertyInfo> props = obj.GetType().GetProperties()
            .Where(x => x.Name == propertyName || x.Name == flagName);

        var propValue = props.FirstOrDefault(x => x.Name == propertyName).GetValue(obj);

        if (props?.Count() == 2 && propValue != null && checkMinValue(propValue.GetType(), propValue))
            props.FirstOrDefault(x => x.Name == flagName).SetValue(obj, true);
    }

    private static bool checkMinValue(Type type, object obj )
    {
        if (type.Name == "DateTime")
            return Convert.ToDateTime(obj) != DateTime.MinValue;
        if (type.Name == "Double")
            return Convert.ToDouble(obj) != double.MinValue;
        if (type.Name == "Int32")
            return Convert.ToInt32(obj) != Int32.MinValue;

        return true;
    }

是的,我知道它可以写得更好,但现在我认为它会起作用:)

【讨论】:

    【解决方案2】:

    我不确定你到底想要什么(请更好地说明你的问题),但我猜是这样的(注意,如果你想检查它们是否为空,你必须让你的 int 和 DateTime 可以为空,因为它们都是是结构):

    public class MyClass
    {
        public int? MyInt { get; set; }
        public bool MyIntSpecified => MyInt.HasValue;
        public DateTime? MyDateTime { get; set; }
        public bool MyDateTimeSpecified => MyDateTime.HasValue;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-23
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 2022-11-22
      • 1970-01-01
      相关资源
      最近更新 更多