reflection.emit

DynamicMethod with generic type parameters

霸气de小男生 提交于 2019-12-29 07:40:06
问题 Is it possible to define a DynamicMethod with generic type parameters? The MethodBuilder class has the DefineGenericParameters method. Does the DynamicMethod have a counterpart? For example is it possible to create method with a signature like the one given blow using DynamicMethod? void T Foo<T>(T a1, int a2) 回答1: Actually there is a way, it's not exactly generic but you'll get the idea: public delegate T Foo<T>(T a1, int a2); public class Dynamic<T> { public static readonly Foo<T> Foo =

DynamicMethod with generic type parameters

风格不统一 提交于 2019-12-29 07:40:05
问题 Is it possible to define a DynamicMethod with generic type parameters? The MethodBuilder class has the DefineGenericParameters method. Does the DynamicMethod have a counterpart? For example is it possible to create method with a signature like the one given blow using DynamicMethod? void T Foo<T>(T a1, int a2) 回答1: Actually there is a way, it's not exactly generic but you'll get the idea: public delegate T Foo<T>(T a1, int a2); public class Dynamic<T> { public static readonly Foo<T> Foo =

System.Reflection.Emit - How to add attribute to return type definition?

随声附和 提交于 2019-12-24 15:42:53
问题 I'm defining some types via System.Reflection.Emit. Imagine that I want to have method signature with some custom attributes, something like this: [return: MyAttr] MyType MethodName([MyOtherAttr] MyOtherType); I use such code to generate it: TypeBuilder t = assembly.DefineType(...); MethodAttributes methodAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot; MethodBuilder method = t.DefineMethod("MethodName", methodAttr, typeof(MyType), new Type[] { typeof

Using nNHibernate with Emitted Code

梦想的初衷 提交于 2019-12-24 13:26:32
问题 I am developing a software that will serve as back-end for a data-ware house, in which fact definitions will be read from an xml file and corresponding fact/dimension tables will be created on the fly. I have managed to make it work i.e. the code creates tables, updates table structure if possible otherwise drop table and make an new one, it can insert data into tables and we can query data-ware house from our client application as well. So far so good. Now I have two problems 1) Too many sql

Casting items of a collection with code generation

偶尔善良 提交于 2019-12-24 08:38:48
问题 I'm doing code generation with C# and I would like to cast a backing field inside a getter. Here is an example: public class Potato { } public class ProxyPotato : Potato { } public class Stew { private ICollection<ProxyPotato> _proxyPotatoes; //This is the code I would like to generate (specialy the cast part) public ICollection<Potato> Potatoes { get { return _proxyPotatoes.Cast<Potato>().ToList(); } } } I have this code which can generate a property but I don't know how to execute a Cast:

How does Reflector decompile code?

半城伤御伤魂 提交于 2019-12-24 07:03:20
问题 How do tools like the Red Gate Ant Profiler or the Reflector convert IL into C# or VB.NET code? I recently noticed that the Red Gate Ant Profiler does not generate the same source code that was written originally. It generated a while loop where I had used a foreach . That made me think. I opened Reflector.exe itself in Reflector but their code is mostly (not all of it) obfuscated. 回答1: Decompilers in general work by looking at the IL and constructing source code which is semantically

how do i write assembly code from c#?

时间秒杀一切 提交于 2019-12-24 05:14:06
问题 i want to write a string of assembly code in c# and have it sent to some win32 api to compile and execute it and get the results back. example: string str = "MOV 1,2;XOR EBP,EBP"... sounds like hard to do but any suggestion would be helpful. 回答1: I doubt there's a win API to compile & execute assembly. Probably the best you could do is write a file, execute MASM (or whatever it's called these days), link it then execute the resulting program. I can't imagine what you are trying to do but this

Selection in GroupBy query with NHibernate with dynamic anonymous object

…衆ロ難τιáo~ 提交于 2019-12-24 03:38:08
问题 My main objective is to create a dynamic group by and use it in NHibernate. Consider this non dynamic example that works : _repository.Collection<User>().GroupBy(u => new { u.Active }).Select(s => s.Key, Count = s.Count()) Now, I create a dynamic object to generate the new { u.Active } part dynamically: private Expression<Func<T, object>> CreateGrouping<T>(IEnumerable<string> by) { var dynamicTypeForGroup = GetDynamicTypeForGroup<T>(by); var sourceItem = Expression.Parameter(typeof(T)); var

copy an attribute of a property from one instance to another instance (of a different type ) at runtime

我怕爱的太早我们不能终老 提交于 2019-12-24 02:23:31
问题 let's say I have 2 classes like this: public class Foo { [Required] public string Name {get;set;} } public class Bar { // put here [Required] at run-time public string Name {get;set;} } var foo = new Foo(); var bar = new Bar(); //copy the required from foo to bar is it possible to copy the attributes from foo to bar at run-time ? 回答1: The notion of "copying" attributes is out. However, you can do something meaningful in the code that checks if the attribute is applied. You could use another

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 =