11

私は持っている

public class Item
{
    public string description { get; set; }
    public string item_uri { get; set; }
    public thumbnail thumbnail { get; set; }
}

public class thumbnail 
{

 private string url { get; set; }
 private string width { get; set; }
 private string height { get; set; }
}

Itemこのようなオブジェクトを作成すると

 Item item = new Item ();

url変数、widthおよびにアクセスするにはどうすればよいheightですか?

ありがとう!

4

5 に答える 5

16

次の 2 つのオプションがあります。

  1. publicの代わりにプロパティを作成しprivateます。
  2. reflectionプロパティにアクセスするために使用します。

(1) を使用することをお勧めします。

また、初期化する必要があることに注意してくださいitem.thumbnail

Item item = new Item ();
item.thumbnail = new thumbnail();

プロパティを常に設定する必要がある場合は、次のようにクラスにコンストラクターを追加できます (サムネイルのセッターも削除し、クラスの名前を大文字にしました。クラス名は大文字で始める必要があります)。thumbnailItemThumbnail

public class Item
{
    public Item(Thumbnail thumbnail)
    {
        if (thumbnail == null)
            throw new ArgumentNullException("thumbnail");

        this.thumbnail = thumbnail;
    }

    public string description { get; set; }
    public string item_uri { get; set; }
    public thumbnail thumbnail { get; }
}

Reflection を使用してプライベート プロパティを取得および設定する

リフレクションを使用するための例を次に示します。次のようなクラスがあるとします。

public class Test
{
    private int PrivateInt
    {
        get;
        set;
    }
}

PrivateInt次のようにプロパティを設定および取得できます。

Test test = new Test();
var privateInt = test.GetType().GetProperty("PrivateInt", BindingFlags.Instance | BindingFlags.NonPublic);

privateInt.SetValue(test, 42); // Set the property.

int value = (int) privateInt.GetValue(test); // Get the property (will be 42).

ヘルパー メソッドで簡素化する

次のような一般的なヘルパー メソッドをいくつか記述することで、これを簡素化できます。

public static T GetPrivateProperty<T>(object obj, string propertyName)
{
    return (T) obj.GetType()
                  .GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic)
                  .GetValue(obj);
}

public static void SetPrivateProperty<T>(object obj, string propertyName, T value)
{
    obj.GetType()
       .GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic)
       .SetValue(obj, value);
}

次に、Testクラスの例は次のようになります。

Test test = new Test();

SetPrivateProperty(test, "PrivateInt", 42);
int value = GetPrivateProperty<int>(test, "PrivateInt");
于 2013-06-02T14:40:32.530 に答える
1

プロパティは、プライベート フィールドの値を読み取り、書き込み、または計算するための柔軟なメカニズムを提供するメンバーです。

したがって、それらをパブリックとして宣言する必要があると思います

public class thumbnail 
{

 public string url { get; set; }
 public string width { get; set; }
 public string height { get; set; }
}

プライベート クラス変数を持つことができ、これらのパブリック プロパティを介してそれらにアクセスできる場合があります。

于 2013-06-02T14:40:58.253 に答える
0

プロパティのサムネイルも新しいオブジェクトに設定する必要があります。

Item item = new Item ();
item.thumbnail = new thumbnail();

item.thumbnail.url = "http://www.microsoft.com";

Item のコンストラクターで設定することもできます。

public class Item
{
    public Item()
    {
        thumbnail = new thumbnail();
    }

    public string description { get; set; }
    public string item_uri { get; set; }
    public thumbnail thumbnail { get; set; }
}

プロパティまたはサムネイル クラスを公開または保護する必要があります。internal を使用することもできますが、internal のスコープは非常に限られています。

public class thumbnail 
{

 protected string url { get; set; }
 protected string width { get; set; }
 protected string height { get; set; }
}

クラスのプライベート プロパティにアクセスするには、リフレクションを使用する必要があります。それについては、この SO の質問を参照してください: .NET Get private property via Reflection

于 2013-06-02T14:39:40.693 に答える