<legend id='j7UVM'><style id='j7UVM'><dir id='j7UVM'><q id='j7UVM'></q></dir></style></legend>

    • <bdo id='j7UVM'></bdo><ul id='j7UVM'></ul>

    <small id='j7UVM'></small><noframes id='j7UVM'>

    <tfoot id='j7UVM'></tfoot>
        <i id='j7UVM'><tr id='j7UVM'><dt id='j7UVM'><q id='j7UVM'><span id='j7UVM'><b id='j7UVM'><form id='j7UVM'><ins id='j7UVM'></ins><ul id='j7UVM'></ul><sub id='j7UVM'></sub></form><legend id='j7UVM'></legend><bdo id='j7UVM'><pre id='j7UVM'><center id='j7UVM'></center></pre></bdo></b><th id='j7UVM'></th></span></q></dt></tr></i><div id='j7UVM'><tfoot id='j7UVM'></tfoot><dl id='j7UVM'><fieldset id='j7UVM'></fieldset></dl></div>

        C#中的函数指针

        Function pointers in C#(C#中的函数指针)

          • <tfoot id='zw3Au'></tfoot>

              <small id='zw3Au'></small><noframes id='zw3Au'>

                <bdo id='zw3Au'></bdo><ul id='zw3Au'></ul>

                <legend id='zw3Au'><style id='zw3Au'><dir id='zw3Au'><q id='zw3Au'></q></dir></style></legend>
                  <tbody id='zw3Au'></tbody>
                <i id='zw3Au'><tr id='zw3Au'><dt id='zw3Au'><q id='zw3Au'><span id='zw3Au'><b id='zw3Au'><form id='zw3Au'><ins id='zw3Au'></ins><ul id='zw3Au'></ul><sub id='zw3Au'></sub></form><legend id='zw3Au'></legend><bdo id='zw3Au'><pre id='zw3Au'><center id='zw3Au'></center></pre></bdo></b><th id='zw3Au'></th></span></q></dt></tr></i><div id='zw3Au'><tfoot id='zw3Au'></tfoot><dl id='zw3Au'><fieldset id='zw3Au'></fieldset></dl></div>
                1. 本文介绍了C#中的函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在某些方面,DelegateMethodInfo 中的一个(或两者)都符合这个标题.但是,两者都没有提供我正在寻找的语法上的好处.所以,简而言之,有什么方法可以写出以下内容:

                  I suppose in some ways either (or both) Delegate or MethodInfo qualify for this title. However, neither provide the syntactic niceness that I'm looking for. So, in short, Is there some way that I can write the following:

                  FunctionPointer foo = // whatever, create the function pointer using mechanisms
                  foo();
                  

                  我不能使用实体委托(即,使用 delegate 关键字来声明委托类型),因为直到运行时才能知道确切的参数列表.作为参考,这是我目前在 LINQPad 中一直在玩的东西,其中 B 将(大部分)是用户生成的代码,Main 也是如此,因此为了美观我的用户,我正在尝试删除 .Call:

                  I can't use a solid delegate (ie, using the delegate keyword to declare a delegate type) because there is no way of knowing till runtime the exact parameter list. For reference, here's what I've been toying with in LINQPad currently, where B will be (mostly) user generated code, and so will Main, and hence for nicety to my users, I'm trying to remove the .Call:

                  void Main()
                  {
                      A foo = new B();
                      foo["SomeFuntion"].Call();
                  }
                  
                  // Define other methods and classes here
                  interface IFunction {
                      void Call();
                      void Call(params object[] parameters);
                  }
                  
                  class A {
                      private class Function : IFunction {
                          private MethodInfo _mi;
                          private A _this;
                          public Function(A @this, MethodInfo mi) {
                              _mi = mi;
                              _this = @this;
                          }
                  
                          public void Call() { Call(null); }
                          public void Call(params object[] parameters) {
                              _mi.Invoke(_this, parameters);
                          }
                      }
                  
                      Dictionary<string, MethodInfo> functions = new Dictionary<string, MethodInfo>();
                  
                      public A() {
                          List<MethodInfo> ml = new List<MethodInfo>(this.GetType().GetMethods());
                          foreach (MethodInfo mi in typeof(Object).GetMethods())
                          {
                              for (int i = 0; i < ml.Count; i++)
                              {
                                  if (ml[i].Name == mi.Name)
                                      ml.RemoveAt(i);
                              }
                          }
                  
                          foreach (MethodInfo mi in ml)
                          {
                              functions[mi.Name] = mi;
                          }
                      }
                  
                      public IFunction this[string function] {
                          get { 
                              if (!functions.ContainsKey(function))
                                  throw new ArgumentException();
                  
                              return new Function(this, functions[function]);
                          }
                      }
                  }
                  
                  sealed class B : A {
                      public void SomeFuntion() {
                          Console.WriteLine("SomeFunction called.");
                      }
                  }
                  

                  推荐答案

                  你说你想保持参数的数量和类型是开放的,但是你可以用一个 delgate 来做到这一点:

                  You say you want to keep the number and type of parameters open, but you can do that with a delgate:

                  public delegate object DynamicFunc(params object[] parameters);
                  

                  这与您目前拥有的完全相同.试试这个:

                  This is exactly the same thing you currently have. Try this:

                  class Program
                  {
                      static void Main(string[] args)
                      {
                          DynamicFunc f = par =>
                                          {
                                              foreach (var p in par)
                                                  Console.WriteLine(p);
                  
                                              return null;
                                          };
                  
                          f(1, 4, "Hi");
                      }
                  }
                  

                  您可以将实例方法委托与您的 Function 类非常相似:一个对象和一个 MethodInfo.所以没必要重写.

                  You can think of an instance-method delegate as very similar to your Function class: an object an a MethodInfo. So there's no need to rewrite it.

                  此外,C 和 C++ 中的函数指针并不符合您的需要:它们不能绑定到对象实例函数,而且它们是静态类型的,而不是动态类型的.

                  Also function pointers in C and C++ are not any closer to what you need: they cannot be bound to an object instance and function, and also they are statically typed, not dynamically typed.

                  如果您想在 DynamicFunc 委托中包装"任何其他方法,请尝试以下操作:

                  If you want to "wrap" any other method in a DynamicFunc delegate, try this:

                  public static DynamicFunc MakeDynamicFunc(object target, MethodInfo method)
                  {
                      return par => method.Invoke(target, par);
                  }
                  
                  public static void Foo(string s, int n)    
                  {
                      Console.WriteLine(s);
                      Console.WriteLine(n);
                  }
                  

                  然后:

                  DynamicFunc f2 = MakeDynamicFunc(null, typeof(Program).GetMethod("Foo"));
                  
                  f2("test", 100);
                  

                  请注意,我使用的是静态方法 Foo,因此我为实例传递 null,但如果它是实例方法,我将传递对象绑定到.Program 恰好是我定义静态方法的类.

                  Note that I'm using a static method Foo so I pass null for the instance, but if it was an instance method, I'd be passing the object to bind to. Program happens to be the class my static methods are defined in.

                  当然,如果你传递了错误的参数类型,那么你会在运行时出错.我可能会寻找一种方法来设计您的程序,以便在编译时捕获尽可能多的类型信息.

                  Of course, if you pass the wrong argument types then you get errors at runtime. I'd probably look for a way to design your program so that as much type information is captured at compile time as possible.

                  这篇关于C#中的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                      <bdo id='En7a3'></bdo><ul id='En7a3'></ul>
                      <legend id='En7a3'><style id='En7a3'><dir id='En7a3'><q id='En7a3'></q></dir></style></legend>
                          <tbody id='En7a3'></tbody>

                          <small id='En7a3'></small><noframes id='En7a3'>

                            <tfoot id='En7a3'></tfoot>
                          1. <i id='En7a3'><tr id='En7a3'><dt id='En7a3'><q id='En7a3'><span id='En7a3'><b id='En7a3'><form id='En7a3'><ins id='En7a3'></ins><ul id='En7a3'></ul><sub id='En7a3'></sub></form><legend id='En7a3'></legend><bdo id='En7a3'><pre id='En7a3'><center id='En7a3'></center></pre></bdo></b><th id='En7a3'></th></span></q></dt></tr></i><div id='En7a3'><tfoot id='En7a3'></tfoot><dl id='En7a3'><fieldset id='En7a3'></fieldset></dl></div>