0

私は(うまくいけば)WCFとJSONを使用してRESTfulサービスを構築しています。

例えば:

[DataContract]
public class Person
{
    // This member is serialized.
    [DataMember]
    internal string FullName;

    // This is serialized even though it is private.
    [DataMember]
    private int Age;

    // This is not serialized because the DataMemberAttribute 
    // has not been applied.
    private string MailingAddress;

    // This is not serialized, but the property is.
    private string telephoneNumberValue;

    [DataMember]
    public string TelephoneNumber
    {
        get { return telephoneNumberValue; }
        set { telephoneNumberValue = value; }
    }
}

WCFメソッドから電話番号のない(つまり、FullNameとAgeのみの)Personオブジェクトを返す必要があるので、PersonクラスとWFCメソッドの属性プロパティのみを使用してそれを行うことができるかどうか疑問に思いました。

ありがとう

v。

4

2 に答える 2

2

Alternately, you could use [DataMember( EmitDefaultValue=false)] and set fields you don't want to serialize to null or not initialize at all

E.g.

[DataContract]
public class Person
{
    // This member is serialized.
    [DataMember]
    internal string FullName;

    [DataMember( EmitDefaultValue=false)]
    public string TelephoneNumber
    {
        get { return telephoneNumberValue; }
        set { telephoneNumberValue = value; }
    }
}

Then you may have 2 methods that load\create Person in 2 different ways

public Person LoadWithPhone() 
    {
        return new Person() { FullName = "Name", TelephoneNumber = "123456" };
    }

 public Person LoadWithoutPhone() 
    {
        return new Person() { FullName = "Name" }; //TelephoneNumber is null
    }

Then when serialize person created by second method, TelephoneNumber will not be serialized This might be useful when load object from DB, you can have several methods that load different columns or have one that is constructing query on the fly and initialized object only with requested fields.

于 2013-08-09T10:47:23.120 に答える
1

The simple answer is you can't do that, but here is an example of a way you can accomplish the same goal without a lot of work.

You will need to have two Person classes, one that has PhoneNumber with the DataMember attribute and one that doesn't. The best way to do this would probably be to create two sub classes from the main Person class

[DataContract]
public class PersonWithoutPhone
{
    private Person _p;

    public PersonWithoutPhone(Person p)
    {
        _p = p;
    }

    [DataMember]
    internal string FullName
    {
        get { return _p.FullName; }
    }

    [DataMember]
    private int Age
    {
        get { return _p.Age; }
    }

    public string TelephoneNumber
    {
        get { return _p.TelephonNumber; }
    }
}

[DataContract]
public class PersonWithPhone
{
    private Person _p;

    public PersonWithoutPhone(Person p)
    {
        _p = p;
    }

    [DataMember]
    internal string FullName
    {
        get { return _p.FullName; }
    }

    [DataMember]
    private int Age
    {
        get { return _p.Age; }
    }

    [DataMember]
    public string TelephoneNumber
    {
        get { return _p.TelephonNumber; }
    }
}
于 2012-08-17T18:54:05.603 に答える