1
votes

I'm trying to access internal property in Silverlight DataGrid using the following code:

 var displayDataType = dataGrid.GetType().GetProperty("DisplayData", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        var displayData = displayDataType.GetValue(dataGrid, null);

But I get following exception

System.MethodAccessException was unhandled by user code Message=Attempt by method 'DataGridDragAndDropSample.MainPage.Button_Click(System.Object, System.Windows.RoutedEventArgs)' to access method 'System.Windows.Controls.DataGrid.get_DisplayData()' failed. StackTrace: at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags) at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, IRuntimeMethodInfo method, RuntimeType parent, UInt32 invocationFlags) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) at DataGridDragAndDropSample.MainPage.Button_Click(Object sender, RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) InnerException:

Is there any other way to access this property in Silverlight DataGrid?

I checked the code with simple sample and it seems to work. Sample Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLib
{
    public class Data
    {
        public Data()
        {
            this.Num = new Num() { Name = "ctor" };
        }

        internal Num Num
        {
            get;
            private set;
        }
    }

    internal class Num
    {
        private string name = string.Empty;
        public string Name
        {
            get
            {
                return this.name;
            }
            internal set
            {
                this.name = value;
            }
        }
    }
}

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Data d = new Data();

            var displayDataType = d.GetType().GetProperty("Num", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            var displayData = displayDataType.GetValue(d, null);


            Console.ReadKey();
        }
    }  
}

Any suggestions?

Regards, Karthik

2
@Jeff Machamer : added exception detailsKarthik

2 Answers

1
votes

From what I've learned about Silverlight and reflection is - what you are trying to do isn't allowed. See this entry: Reflection restrictions in Silverlight

I've used Reflector a lot to exam the DataGrid (as well as the code itself from CodePlex), to find ways to access parts that are not exposed easily... kind of frustrating that so many things are internal on the DataGrid (and related classes). I have not found a way to access the DisplayData property that you are interested in. You might want to look over the code a see if you can find another way to get what you want.

0
votes

when property is internal

user code must be enabled to have

reflection permission

with silver light all limits are

in place to prevent the abuse

is not present; possible