Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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!

like image 725
RonaDona Avatar asked Jun 02 '13 14:06

RonaDona


People also ask

How do I access private property of a class?

Privates should start with # . They are only accessible from inside the class. On the language level, # is a special sign that the field is private. We can't access it from outside or from inheriting classes.

Can properties be private?

Properties can be marked as public , private , protected , internal , protected internal , or private protected . These access modifiers define how users of the class can access the property.

What are private properties in JavaScript?

The private keyword in object-oriented languages is an access modifier that can be used to make properties and methods only accessible inside the declared class. This makes it easy to hide underlying logic that should be hidden from curious eyes and should not be interacted with outside from the class.


2 Answers

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");
like image 108
Matthew Watson Avatar answered Oct 14 '22 15:10

Matthew Watson


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.

like image 42
Sachin Avatar answered Oct 14 '22 15:10

Sachin