C# reflection GetValue from a field in generic base class -


The problem is that we can not get the gateway of a field (non-generic) which remains only in the base class, in which The common type is please see the code snippet below. Calling

  f.GetValue (a)   

will throw exceptions with the message: Can not be used on fields with late-bound operational types, What kind of type Containsensen generic parameters are true.

  class program {static zero main (string [] args) {type abstractGenericType = typeof (ClassB <>;) FieldInfo [] fieldInfos = abstractGenericType.GetFields (BindingFlags.Public | BindingFlags.Instance); Class AA = New Class ("Hello"); Foreach (fees in fieldInfo field) {f.GetValue (a); // Invalid Operation conviction}}} Internal class ClassB   

Our design requires that we get FieldInfo first before any instance of real objects. Therefore we can not use

  type type A = abstractGenericType.MakeGenericType (typeof (string)); FieldInfo [] fieldInfos = typeA.GetFields ();   

thanks

I think it comes to this problem Normal classes are dynamically compiled with a specific type, a common type can also be defined such as

  internal class Class B < T & gt; {Public T-value; Public string string; Public class (string s) {str = s; }}   

You may have trouble obtaining the value of the field "value" by using the workaround gatetip (), or by typing the new base class without generic arguments Using which you want the field.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -