0

I want to get value of Rectangle as you can see in the picture below,

enter image description here

I did some googling but only thing I can find is that I can access a property but not a object,

I need to access a non-public member (Highlighted Item) of a Combo Box

I am using Aspose.Pdf.Cell by the way.

Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • 1
    It is private for a reason. You may want to describe what you intend to accomplish by modifying it. Generally, when a member is private and it has to do with layout or arrangement, using reflection or other techniques to directly change the value will not have the intended effect. – Will Custode Feb 10 '15 at 12:52
  • @WilliamCustode just want to read its value - and here's the real reason http://www.aspose.com/community/forums/thread/605765/as-we-have-processparagraphs-now-can-we-get-rectangle-out-of-cell-now.aspx – Mathematics Feb 10 '15 at 12:53
  • The question you have linked to does return the properties object with reflection, what doesn't work with that? – Sayse Feb 10 '15 at 12:57
  • Once you have the `PropertyInfo` you can access to the value by call the method `GetValue(Object object);` More info at [MSDN Article](https://msdn.microsoft.com/en-us/library/hh194385(v=vs.110).aspx) – Gianni B. Feb 10 '15 at 12:59

3 Answers3

7

Rectangle here seems to be a public property of the class Aspose.Pdf.Text.TextFragment. So, probably you can use type cast to get it:

var fragment = d.Paragraphs[0] as TextFragment;
if(fragment!=null) 
{
    var rect = fragment.Rectangle
}

I didn't try it, but according to documentation both Paragraphs indexer and Rectangle property are public. "non-public members" message in the IDE concerns inner array of Paragrpaphs object which is accessed via reflection in this debugger session.

default locale
  • 13,035
  • 13
  • 56
  • 62
2
var paragraph = d.Paragraphs.FirstOrDefault(); // or [0] if you're not using Linq
var pInfo = paragraph.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(p => p.Name == "Rectangle");
if(pInfo != null)
{
    var val = pInfo.GetValue(paragraph) as Rectangle; // or whatever the actual type of that property is
}

Using reflection allows you to read and write values that you may not have access to. The only caveat here is that it cannot be a private member.

But if this property is public, as explained above, you should absolutely go through the actual object's exposed API and not do this.

Will Custode
  • 4,576
  • 3
  • 26
  • 51
0

As @William-Custode noted, interacting with private properties can have unintended side effects since they are often made private for a reason. However, sometimes it's necessary for debugging or testing purposes. If you need to access the value of one, you can do so via reflection mechanisms, as in the following:

using System;
using System.Reflection;

public class Example
{
    public int PublicProp { get { return 10; } }

    private int PrivateProp { get { return 5; } }
}

public class Program
{
    public static void Main()
    {
        Example myExample = new Example();

        Console.WriteLine("PublicProp: " + myExample.PublicProp);

        PropertyInfo[] properties = typeof(Example).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);

        foreach (PropertyInfo prop in properties)
        {
            Console.WriteLine(prop.Name + ": " + prop.GetValue(myExample));
        }

        Console.ReadKey();
    }
}
dshockey
  • 185
  • 1
  • 10