dynamic-method

Is it possible to invoke internal method from a dynamic method in .NET?

浪子不回头ぞ 提交于 2020-01-13 08:41:49
问题 I am trying to invoke an internal method from a dynamically generated one. The il code is simple: ldarg_0, callvirt, ret. Executing the method fails with TypeLoadException saying it cannot load the type on which the internal method is defined. When I think of it, this seems logical, because the dynamic method host assembly is not a friend of the method's declaring type assembly. However, I have expected the dynamic method still to work, just like Delegate.CreateDelegate works. After all, I

Generate dynamic method to set a field of a struct instead of using reflection

孤者浪人 提交于 2020-01-09 09:07:31
问题 Let's say I have the following code which update a field of a struct using reflection. Since the struct instance is copied into the DynamicUpdate method, it needs to be boxed to an object before being passed. struct Person { public int id; } class Test { static void Main() { object person = RuntimeHelpers.GetObjectValue(new Person()); DynamicUpdate(person); Console.WriteLine(((Person)person).id); // print 10 } private static void DynamicUpdate(object o) { FieldInfo field = typeof(Person)

Comparing all properties of an object using expression trees

社会主义新天地 提交于 2019-12-24 02:13:13
问题 I'm trying to write a simple generator that uses an expression tree to dynamically generate a method that compares all properties of an instance of a type to the properties of another instance of that type. This works fine for most properties, like int an string , but fails for DateTime? (and presumably other nullable value types). The method: static Delegate GenerateComparer(Type type) { var left = Expression.Parameter(type, "left"); var right = Expression.Parameter(type, "right");

Is there a way to dynamically call a method or set an instance variable in a class in Dart?

我只是一个虾纸丫 提交于 2019-12-23 15:42:44
问题 I would want to be able to do something like this with a Dart class constructor: class Model { // ... setting instance variables Model(Map fields) { fields.forEach((k,v) => this[k] = v); } } Obviously, this doesn't work, because this doesn't have a []= method. Is there a way to make it work or is it simply not "the dart way" of doing things? If it's not, could you show me what would be the right way to tackle this? 回答1: Currently no. You will have to wait for reflection to arrive in Dart

Ruby send method with rails associations

杀马特。学长 韩版系。学妹 提交于 2019-12-11 18:07:13
问题 I have been messing about with making a sortable table module thing. I know some might exist but want to get experience doing this myself. I had the idea of have it like so: SortedTable.new(ModelName, Hash_Of_Columns_And_Fields, ID) example SortedTable.new(Post, {"Title" => "title", "Body" => "body", "Last Comment" => "comment.last.title"}, params[:id]) I am planning to do something like: def initialize(model, fields, id) data = {} model = model.capitalize.constantize model.find(id) fields

Is it possible to invoke internal method from a dynamic method in .NET?

只谈情不闲聊 提交于 2019-12-05 02:16:46
I am trying to invoke an internal method from a dynamically generated one. The il code is simple: ldarg_0, callvirt, ret. Executing the method fails with TypeLoadException saying it cannot load the type on which the internal method is defined. When I think of it, this seems logical, because the dynamic method host assembly is not a friend of the method's declaring type assembly. However, I have expected the dynamic method still to work, just like Delegate.CreateDelegate works. After all, I did manage to get the MethodInfo of the internal method, so the permissions barrier are behind me. Anyway

Dynamic method calling in Ruby

血红的双手。 提交于 2019-11-28 16:18:05
As far as I am aware there are three ways to dynamically call a method in Ruby: Method 1: s = SomeObject.new method = s.method(:dynamic_method) method.call Method 2: s = SomeObject.new s.send(:dynamic_method) Method 3: s = SomeObject.new eval "s.dynamic_method" By benchmarking them I have established that Method 1 is by far the fastest, Method 2 is slower, and Method 3 is by far the slowest. I have also found that .call and .send both allow calling private methods, while eval does not. So my question is: is there any reason to ever use .send or eval ? Why would you not always just use the

Generate dynamic method to set a field of a struct instead of using reflection

∥☆過路亽.° 提交于 2019-11-28 06:04:05
Let's say I have the following code which update a field of a struct using reflection. Since the struct instance is copied into the DynamicUpdate method, it needs to be boxed to an object before being passed . struct Person { public int id; } class Test { static void Main() { object person = RuntimeHelpers.GetObjectValue(new Person()); DynamicUpdate(person); Console.WriteLine(((Person)person).id); // print 10 } private static void DynamicUpdate(object o) { FieldInfo field = typeof(Person).GetField("id"); field.SetValue(o, 10); } } The code works fine. Now, let's say I don't want to use

Dynamic method calling in Ruby

萝らか妹 提交于 2019-11-27 09:38:55
问题 As far as I am aware there are three ways to dynamically call a method in Ruby: Method 1: s = SomeObject.new method = s.method(:dynamic_method) method.call Method 2: s = SomeObject.new s.send(:dynamic_method) Method 3: s = SomeObject.new eval "s.dynamic_method" By benchmarking them I have established that Method 1 is by far the fastest, Method 2 is slower, and Method 3 is by far the slowest. I have also found that .call and .send both allow calling private methods, while eval does not. So my