Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

Reflection with TypeBuilder adding duplicate field in a type.

$
0
0

I have a problem, im trying to dynamically create a type using .Net reflection and c#.

Using the the TypeBuilder object i create field in the specified type :

TypeBuilder oObjectBuilder = (TypeBuilder)oClassObject;

FieldBuilder oFieldBuilder = oObjectBuilder.DefineField(strFieldName,oFieldType,FieldAttributes.Public);

My problem is that DefineField does not trow an exception if there is a field already defined with the same name "strFieldName" and theres not way to detect if the field already exist before creating it

FindMembers and GetField trow a NotSupported Exception as its not a RuntimeType and still in definition because i havent call the CreateType function.

I could bypass this problem by keeping an hashtable or an array we the already defined field name but this add unecessary overhead in my code.

Anyone have a solution with that or know how to do it wiht the TypeBuilder object ?I

There should be a function that return the list of current field of current type in definition even if it wasnt created first with the CreateType. 

I appreciate any help . Thank you


Viewing all articles
Browse latest Browse all 1710

Trending Articles



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