RonaDona
RonaDona

Reputation: 932

How can I access some (private) properties of an object?

I have

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

and

public class thumbnail 
{

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

If I create an object of Item like this

 Item item = new Item ();

How can I access the variables url, width and height?

Thanks!

Upvotes: 13

Views: 17773

Answers (5)

infinitesearch
infinitesearch

Reputation: 1

Alternatively you can do:

public class thumbnail 
{
   private string url { get; private set; }
   private string width { get; private set; }
   private string height { get; private set; }
}

This will allow you to read but not write.

Upvotes: 0

Matthew Watson
Matthew Watson

Reputation: 109567

You have two options:

  1. Make the properties public instead of private.
  2. Use reflection to access the properties.

I recommend using (1).

Note that you also need to initialise item.thumbnail:

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

If you require that the thumbnail property is always set, you could add a constructor to class Item as follows (where I have also removed the setter for thumbnail and capitalised the name of the Thumbnail class. Class names should begin with a capital letter):

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; }
}

Using Reflection to get and set the private properties

To use reflection, here's an example. Given a class like this:

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

You can set and get its PrivateInt property like so:

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).

Simplify with helper methods

You could simplify this by writing a couple of generic helper methods like so:

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);
}

Then the example with the Test class would be like this:

Test test = new Test();

SetPrivateProperty(test, "PrivateInt", 42);
int value = GetPrivateProperty<int>(test, "PrivateInt");

Upvotes: 19

Chris Gessler
Chris Gessler

Reputation: 23113

You need to set the property thumbnail to a new object as well.

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

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

You could also have the constructor of Item set it.

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

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

You should make the properties or the thumbnail class public or protected. You could also use internal, but internal has a very limited scope.

public class thumbnail 
{

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

To access private properties of the class, you will have to use reflection. See this SO question for that: .NET Get private property via Reflection

Upvotes: 0

John Woo
John Woo

Reputation: 263713

You cannot access them because they are private properties. The way you can access it is by accessing them internally by the member of thumbnail itself.

If you want them to be accessed outside the class, make the members public.

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

Upvotes: 0

Sachin
Sachin

Reputation: 40970

Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields

So I think you should declare them as public

public class thumbnail 
{

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

May be you can have private class variables and then you can access them via these public properties.

Upvotes: 1

Related Questions