reflection - Function pointers in C# -


I think that in some ways (or both) delegate or MethodInfo Qualify for this title However, neither provide syntax eligibility which I see. So, in short, is there any way that I can write the following:

  function pointer foo = // Whatever, create fuses indicator using the fuses indicator;  

I can not use a concrete representative (i.e., using the delegate to declare a representative type) because anyone knows the exact till runtime There is no way for the parameter list reference, here I am currently in LINQPad, where B would be (mostly) user-generated code, and such would be the main And so precisely my user, I have to remove I am trying. Call :

  void main () {a foo = new B (); Foo ["SomeFuntion"] call (). } // defined other methods and classes here interface iFunction {zero call (); Zero call (parameter object [] parameter); } Category A {Private class function: IFunction {Private MethodInfo _mi; Private A _this; Public function (A @ this, legalInfo) {_mi = mi; _this = @this; } Public Zero Calls () {Call (zero); } Public Zero Call (parameter object [] parameter) {_mi.Invoke (_this, parameter); }} Dictionary & lt; String, MethodInfo & gt; Work = new dictionary & lt; String, MethodInfo & gt; (); Public A () {List & lt; MethodInfo & gt; Found = new list & lt; Method Info & gt; (GetType (. GetMethods ()); The front (the type of object in the method MyGetMethods ()) {for (int i = 0; i & lt; ml.Count; i ++) {if (mL [i] .name == mi.Name) ml .RemoveAt i); }} Foreach (methodInfo mi mL) {function [mi.Name] = mi; }} Public IFunction this [string function] {get (ifctions! Functions.ContainsKey (function)) New ArgumentException (); Come back new function (this, function [function]);}}} Seal word b: A {public Zero Some Functions () {Console.WriteLine ("Somefunction.");}}  

< P> You say that you want to keep numbers and parameters open, but you can do this with a delgate:

  Public representative object DynamicFunc (params object [] parameter); < / Code> 

Try it out:

  class program {static zero main (string [] args) {DynamicFunc f = par => {Foreach (equal to equal) Console.WrightLine (P); Return Null;}; F (1, 4, "Hi");}}  

You can set your function class: an object You can think of an example-method representative similar to a MethodInfo , so there is no need to re-write it.

Points in C and C ++ also work which are not essential to you, they can not be bound to an object example and function, and they are still static Are typed in forms, not dynamically typed.

If you want to wrap any other method in the "DynamicFunc Representative", then try it:

  Public Stable DynamicFunc MakeDynamicFunc Object Target, MethodInfo Method) {return Par = & gt; Method.Invoke (Target, Equal); } Public static zero Foo (strings, int n) {Console.WriteLine (S); Console.WriteLine (N); }  

and after that:

  dynamicfunction F2 = macdinifixed (empty, typef (program). Gatemathi ("foo")); F2 ("test", 100);  

Note that I am using a static method Foo , so I pass null for example, but If this was an example method, I want to pass the object to bind program in the class, my static methods are defined.

Of course, if you pass wrong logic types then you get errors at the runtime. I will probably find a way to design my program so that information can be collected at as much time as possible.


Comments

Popular posts from this blog

email - PHP mail error ... failed to open stream : permission denied -

c# - ListView onScroll event -

c - Linux mmap() error -