【问题标题】:How do I prevent web service from auto generating properties from fields in reference.cs如何防止 Web 服务从 reference.cs 中的字段自动生成属性
【发布时间】:2018-04-20 04:00:35
【问题描述】:

我遇到了一个问题,旧的 win 表单应用程序中的一些代码使用 Web 服务中的对象反射来获取字段列表。反射正在调用 GetFields()。在 Web 服务的数据对象中,我定义了如下字段:

public string PropertyAddress1;
public string PropertyAddress2;
public string PropertyCity;
public string PropertyCounty;
public string PropertyState;

在 win 表单应用程序中存在的当前版本的 reference.cs 中,字段如下所示。

public string PropertyAddress1;
public string PropertyAddress2;
public string PropertyCity;
public string PropertyCounty;
public string PropertyState;

也一样吗?

好吧,我进入了 Web 服务的课程并添加了另一个字段。然后我进入win forms应用程序,右键单击并点击更新网络参考。重新生成代码后,reference.cs 如下所示:

private string propertyAddress1Field;
private string propertyAddress2Field;
private string propertyCityField;
private string propertyCountyField;
private string propertyStateField;

   public string PropertyAddress1 {
        get {
            return this.propertyAddress1Field;
        }
        set {
            this.propertyAddress1Field = value;
        }
    }

    /// <remarks/>
    public string PropertyAddress2 {
        get {
            return this.propertyAddress2Field;
        }
        set {
            this.propertyAddress2Field = value;
        }
    }

    /// <remarks/>
    public string PropertyCity {
        get {
            return this.propertyCityField;
        }
        set {
            this.propertyCityField = value;
        }
    }

    /// <remarks/>
    public string PropertyCounty {
        get {
            return this.propertyCountyField;
        }
        set {
            this.propertyCountyField = value;
        }
    }

    /// <remarks/>
    public string PropertyState {
        get {
            return this.propertyStateField;
        }
        set {
            this.propertyStateField = value;
        }
    }

这是抛弃现有代码的行为,因为 GetFields() 不再返回任何内容。我知道我可以将它交换为 GetProperties() 但该类已经定义了我不希望在结果集中返回的属性。我希望参考 .cs 将字段生成为字段并将属性生成为属性,而不是将字段转换为属性。有什么方法可以在字段的自动生成的 reference.cs 中放置一些数据注释或其他类型的标志来防止这种行为?

【问题讨论】:

    标签: c# web-services reflection


    【解决方案1】:

    您可以尝试手动更新 reference.cs 文件以将属性替换为字段。如果引用尚未以某种方式配置为使用字段而不是属性,我愿意打赌这就是以前的作者所做的。

    【讨论】:

    • 我也有这样的感觉。我还听说有早期版本的 .net 或 Visual Studio,自动生成器引擎可能没有自动将字段转换为属性,我希望有一个设置可以控制这一点。
    猜你喜欢
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 2010-12-19
    • 2013-08-15
    相关资源
    最近更新 更多