This project has moved and is read-only. For the latest updates, please go here.

How to access the field like System.Data.RelatedView.values ?

Topics: User Forum
Nov 26, 2007 at 7:33 PM

I'm search how to use your generic based framework on a Type which is inaccessible like System.Data.RelatedView ?

Thanks for your great dynamic Reflection Lib.

Nov 27, 2007 at 11:50 PM
Thanks for the interest.... the fact that a type is internal or private should cause you no trouble if you are running under full trust. Can you upload or send me a sample that breaks, I'll happily fix the library to accomodate.
Dec 4, 2007 at 11:18 AM
The problem is that the type System.Data.RelatedView is internal into System.Data.dll so the following code can't be compiled :

so I've wrote this to access it without your lib:

Type relatedViewType = System.Reflection.Assembly.GetAssembly(typeof(System.Data.DataSet))

FieldInfo valuesField = relatedViewType.GetField("values", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

//build setter
DynamicMethod setterDef = new DynamicMethod(relatedViewType.Name + "" + valuesField + "Setter",
new Type[] { typeof(DataView), typeof(object[]) }, //define 1st param as DataView cause RelatedView is not an accessible type

ILGenerator setterIL = setterDef.GetILGenerator();
setterIL.Emit(OpCodes.Ldarg_0); //we expect in top of the stack object instance
setterIL.Emit(OpCodes.Stfld, valuesField);
setterIL.Emit(OpCodes.Ret); //note: we have to return, even if method is void

//assign s_RelatedViewKeyValuesSetter with the builded method
s_RelatedViewKeyValuesSetter = (Common.Utility.CodeEmit.Proc<DataView, object[]>)setterDef.CreateDelegate(typeof(Common.Utility.CodeEmit.Proc<DataView, object[]>));