Quantcast
Channel: Dynamic Reflection Library
Viewing all articles
Browse latest Browse all 15

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

$
0
0
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[]>));



Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>