This project has moved. For the latest updates, please go here.

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

Topics: User Forum
Nov 26, 2007 at 6:33 PM
Hello,

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.

BxB
Coordinator
Nov 27, 2007 at 10: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 10: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 :
Dynamic<System.Data.RelatedView>.Instance.Field<object[]>.Getter.CreateDelegate("values");

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

Type relatedViewType = System.Reflection.Assembly.GetAssembly(typeof(System.Data.DataSet))
.GetType("System.Data.RelatedView");

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

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

ILGenerator setterIL = setterDef.GetILGenerator();
setterIL.Emit(OpCodes.Ldarg_0); //we expect in top of the stack object instance
setterIL.Emit(OpCodes.Ldarg_1);
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[]>));