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