dynamic-invoke

Is there a way to do dynamic implicit type casting in C#?

删除回忆录丶 提交于 2019-12-25 04:02:27
问题 Given this class with an implicit cast operator: public class MyDateTime { public static implicit operator MyDateTime(System.Int64 encoded) { return new MyDateTime(encoded); } public MyDateTime(System.Int64 encoded) { _encoded = encoded; } System.Int64 _encoded; } I can now do the following: long a = 5; MyDateTime b = a; But NOT the following: long f = 5; object g = f; MyDateTime h = g; This gives a compile time: Cannot implicitly convert type 'object' to 'MyDateTime'. Makes sense to me. Now

Puzzle involving unwound stacks on dynamic invoke

一笑奈何 提交于 2019-12-24 08:42:45
问题 This is a new attempt to pose a version of a question asked less successfully this morning. Consider the following program, which we'll run once inside Visual Studio 2010 and once more by double-clicking the executable directly namespace ConsoleApplication3 { delegate void myFoo(int i, string s); class Program { static void Main(string[] args) { Foo(1, "hello"); Delegate Food = (myFoo)Foo; Food.DynamicInvoke(new object[] { 2, null }); } static void Foo(int i, string s) { Console.WriteLine("If

Is there a way to do dynamic implicit type casting in C#?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 16:28:02
问题 Given this class with an implicit cast operator: public class MyDateTime { public static implicit operator MyDateTime(System.Int64 encoded) { return new MyDateTime(encoded); } public MyDateTime(System.Int64 encoded) { _encoded = encoded; } System.Int64 _encoded; } I can now do the following: long a = 5; MyDateTime b = a; But NOT the following: long f = 5; object g = f; MyDateTime h = g; This gives a compile time: Cannot implicitly convert type 'object' to 'MyDateTime'. Makes sense to me. Now

How do I pass an object array into a method as individual parameters?

人走茶凉 提交于 2019-12-13 01:27:24
问题 I have the following code: public class MyClass { private Delegate m_action; public object[] m_args; public MyClass() { } public MyClass(Delegate action, params object[] args) { m_args = args; m_action = action; } public void Execute() { m_action.DynamicInvoke(m_args); } } The problem with this approach is that the m_args is an object itself, and its contents are not being flattened out into individual params entries. How can I fix this? 回答1: I think you are mistaken. The params seems to work

alternative for using slow DynamicInvoke on muticast delegate

落爺英雄遲暮 提交于 2019-12-07 09:57:58
问题 I have the following piece of code in a base-class: public static void InvokeExternal(Delegate d, object param, object sender) { if (d != null) { //Check each invocation target foreach (Delegate dDelgate in d.GetInvocationList()) { if (dDelgate.Target != null && dDelgate.Target is System.ComponentModel.ISynchronizeInvoke && ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).InvokeRequired) { //If target is ISynchronizeInvoke and Invoke is required, invoke via ISynchronizeInvoke (

alternative for using slow DynamicInvoke on muticast delegate

浪子不回头ぞ 提交于 2019-12-05 14:39:14
I have the following piece of code in a base-class: public static void InvokeExternal(Delegate d, object param, object sender) { if (d != null) { //Check each invocation target foreach (Delegate dDelgate in d.GetInvocationList()) { if (dDelgate.Target != null && dDelgate.Target is System.ComponentModel.ISynchronizeInvoke && ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).InvokeRequired) { //If target is ISynchronizeInvoke and Invoke is required, invoke via ISynchronizeInvoke ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).Invoke(dDelgate, new object[] { sender,

Can Delegate.DynamicInvoke be avoided in this generic code?

萝らか妹 提交于 2019-12-04 11:01:20
问题 This question is partly about delegates, and partly about generics. Given the simplified code: internal sealed class TypeDispatchProcessor { private readonly Dictionary<Type, Delegate> _actionByType = new Dictionary<Type, Delegate>(); public void RegisterProcedure<T>(Action<T> action) { _actionByType[typeof(T)] = action; } public void ProcessItem(object item) { Delegate action; if (_actionByType.TryGetValue(item.GetType(), out action)) { // Can this call to DynamicInvoke be avoided? action

Is there a way to do dynamic implicit type casting in C#?

送分小仙女□ 提交于 2019-11-27 22:59:57
Given this class with an implicit cast operator: public class MyDateTime { public static implicit operator MyDateTime(System.Int64 encoded) { return new MyDateTime(encoded); } public MyDateTime(System.Int64 encoded) { _encoded = encoded; } System.Int64 _encoded; } I can now do the following: long a = 5; MyDateTime b = a; But NOT the following: long f = 5; object g = f; MyDateTime h = g; This gives a compile time: Cannot implicitly convert type 'object' to 'MyDateTime'. Makes sense to me. Now I modify the previous example as follows: long f = 5; object g = f; MyDateTime h = (MyDateTime)g; This

What is the __DynamicallyInvokable attribute for?

扶醉桌前 提交于 2019-11-27 17:28:28
Looking through System.Linq.Enumerable in DotPeek I notice that some methods are flavoured with a [__DynamicallyInvokable] attribute. What role does this attribute play? Is it something added by DotPeek or does it play another role, perhaps informing the compiler on how best to optimise the methods? It is undocumented, but it looks like one of the optimizations in .NET 4.5. It appears to be used to prime the reflection type info cache, making subsequent reflection code on common framework types run faster. There's a comment about it in the Reference Source for System.Reflection.Assembly.cs,

What is the __DynamicallyInvokable attribute for?

て烟熏妆下的殇ゞ 提交于 2019-11-26 18:59:55
问题 Looking through System.Linq.Enumerable in DotPeek I notice that some methods are flavoured with a [__DynamicallyInvokable] attribute. What role does this attribute play? Is it something added by DotPeek or does it play another role, perhaps informing the compiler on how best to optimise the methods? 回答1: It is undocumented, but it looks like one of the optimizations in .NET 4.5. It appears to be used to prime the reflection type info cache, making subsequent reflection code on common