typebuilder

Convert from Type to TypeBuilder

我的未来我决定 提交于 2020-01-26 01:16:50
问题 I'm creating a class on runtime and some of the types are already created inside the ModuleBuilder and I would like to reused them, but I only have the Type and not the TypeBuilder (Which is what I need in order to change it) Is there a way to convert from Type to TypeBuilder? Type moduleType = ModuleBuilder.GetType(inXmlTemplateProperty.PropertyName); if (moduleType == null) { TypeBuilder newClass = ModuleBuilder.DefineType(inXmlTemplateProperty.PropertyName, TypeAttributes.Public |

Reflection Emit: how to build constructor for this

我们两清 提交于 2019-12-23 16:02:43
问题 The code I want to build dynamically is as follow: public class Sample { public Sample() { Items = new ObservableTestCollection<Sample>(this); } public Sample(IEnumerable<Sample> source) { Items = new ObservableTestCollection<Sample>(this, source); } public ObservableTestCollection<Sample> Items; } The Source of ObservableTestCollection is as follow: public class ObservableTestCollection<T> : ObservableCollection<T> { public T Parent; public ObservableTestCollection(T parent) { Parent =

TypeBuilder - Adding attributes

感情迁移 提交于 2019-12-21 23:37:06
问题 I have a helper class that uses a TypeBuilder to construct a dynamic type. It is used as follows : var tbh = new TypeBuilderHelper("MyType"); tbh.AddProperty<float>("Number", 0.0f); tbh.AddProperty<string>("String", "defaultStringValue"); tbh.Close(); var i1 = tbh.CreateInstance(); var i2 = tbh.CreateInstance(); I now want to add support for property attributes (existing attribute types, not dynamically generated types), along the lines of : public class TypeBuilderHelper { public void

Creating dynamic type from TypeBuilder with a base class and additional fields generates an exception

匆匆过客 提交于 2019-12-21 10:43:39
问题 I am trying to create a dynamic type based on an existing type that contains only public fields. The new dynamic type must also inherit from a different base type which only has a fully implemented method. I create the TypeBuilder specifying the base type then I add the public fields to it and finally I call CreateType() . The resulting error message is: "Could not load type 'InternalType' from assembly 'MyDynamicAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because field

How to: Define a Self Referenced Type Property with Reflection Emit in c#

一个人想着一个人 提交于 2019-12-09 19:13:46
问题 How to define self referenced collection property ? Type I want to build with Reflection Type Builder. public class Sample { public Sample() { Items = new List<Sample>(); } public List<Sample> Items { get; set; } Public void AddSample(Sample item) { items.Add(item); } } The code I write AppDomain myDomain = AppDomain.CurrentDomain; AssemblyName myAsmName = new AssemblyName("GenericEmit"); AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave);

Generating a constructor that takes an argument and just calls base(argument) using TypeBuilder

穿精又带淫゛_ 提交于 2019-12-07 05:19:19
问题 I am trying to dynamically create an empty constructor that takes an argument and simply calls base(argument) using TypeBuilder. My code is something like: (...) // Create a class derived from this class TypeBuilder typeBuilder = moduleBuilder.DefineType("NewClass", TypeAttributes.Class, this.GetType()); ConstructorInfo baseCtor = this.GetType().GetConstructor(new[] { typeof(int) }); // Define new ctor taking int ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor

TypeBuilder - Adding attributes

坚强是说给别人听的谎言 提交于 2019-12-04 20:18:22
I have a helper class that uses a TypeBuilder to construct a dynamic type. It is used as follows : var tbh = new TypeBuilderHelper("MyType"); tbh.AddProperty<float>("Number", 0.0f); tbh.AddProperty<string>("String", "defaultStringValue"); tbh.Close(); var i1 = tbh.CreateInstance(); var i2 = tbh.CreateInstance(); I now want to add support for property attributes (existing attribute types, not dynamically generated types), along the lines of : public class TypeBuilderHelper { public void AddProperty<T>(string name, T defaultValue, params Attribute[] attributes) { // ... } } public class

How to: Define a Self Referenced Type Property with Reflection Emit in c#

青春壹個敷衍的年華 提交于 2019-12-04 14:14:53
How to define self referenced collection property ? Type I want to build with Reflection Type Builder. public class Sample { public Sample() { Items = new List<Sample>(); } public List<Sample> Items { get; set; } Public void AddSample(Sample item) { items.Add(item); } } The code I write AppDomain myDomain = AppDomain.CurrentDomain; AssemblyName myAsmName = new AssemblyName("GenericEmit"); AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder myModule = myAssembly.DefineDynamicModule(myAsmName.Name, myAsmName.Name + ".dll");

Creating dynamic type from TypeBuilder with a base class and additional fields generates an exception

痴心易碎 提交于 2019-12-04 03:52:44
I am trying to create a dynamic type based on an existing type that contains only public fields. The new dynamic type must also inherit from a different base type which only has a fully implemented method. I create the TypeBuilder specifying the base type then I add the public fields to it and finally I call CreateType() . The resulting error message is: "Could not load type 'InternalType' from assembly 'MyDynamicAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because field 'first' was not given an explicit offset." To me this implies that the CreateType method is looking for

Using TypeBuilder to create a pass-through constructor for the base class

此生再无相见时 提交于 2019-11-29 11:42:22
问题 Say I have a SpaceShip class, like so: public class SpaceShip { public SpaceShip() { } public SpaceShip(IRocketFuelSource fuelSource) { } } I want to use TypeBuilder to create a type at run-time which inherits from SpaceShip, and defines one constructor for each of the ones in SpaceShip. I don't need the constructors to actually do anything except pass their arguments up to the parent ("pass-through" constructors). For example, the generated type would look something like this if expressed in