-1

I am aware there are a couple of questions that ask about exactly the same thing as this, however, they don't do what I want. I want something like JavaScript's .call(). The closest analog to that is Delegate.DynamicInvoke() but I don't know how I can convert the MethodInfo object I have to a delegate because I don't have a delegate type to use in MethodInfo.CreateDelegate(). I see there is a _MethodInfo.Invoke() but it seems very complicated.

Essentially I want something like this, but in C#:

// lets just assume this is an actual class or something
someclass.prototype.something = function(otherName) {
    console.log(this.name, otherName);
}
let obj = {
    name: 'words'
};
something.call(obj, 'more words');
VeryGoodDog
  • 335
  • 2
  • 11
  • 1
    "it seems very complicated" - have you *tried* to use it? What happened? You specify a target and the arguments for the target... that doesn't seem terribly complicated to me, but we can't tell what you're finding complicated about it without knowing what you've tried and what happened. – Jon Skeet Jun 08 '21 at 15:19
  • 1
    So what problems did you have calling invoke? What did you attempt, and how did it fail to accomplish what you want it to do? – Servy Jun 08 '21 at 15:19
  • 2
    What is complicated about `MethodInfo.Invoke()`? I am no expert in js but `call` looks very similar in usage to the `Invoke`. For `MethodInfo.Invoke()` - first parameter is the object on which to invoke the method (use `null` if method is static) (i.e. `obj` in your code) and second parameter - an array containing all "other" method parameters (i.e. `new []{"more words"}` will be analog to your js code). Also you can always check out the [docs](https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodinfo.invoke?view=netframework-1.1), sometimes the can shed some light on the usage. – Guru Stron Jun 08 '21 at 15:22
  • Ok, I feel like a clown. On the MSDN `MethodInfo` docs the left sidebar doesn't have `MethodInfo.Invoke()` because it's inherited from `MethodBase`, it only has `_MethodInfo.Invoke()` which requires a handful of pointers (I think). I'll use `MethodInfo.Invoke()` and write a new answer about that because the other ones answers were several years old, or whatever. – VeryGoodDog Jun 08 '21 at 15:31
  • Nothing in C# will be directly analogous to the JavaScript you posted, because in C# an instance method is tied directly to the type that it's declared on. You cannot call an instance method on anything except an instance of the type it's declared on. However, if you're willing to either ensure the object is of the right type (`let obj = new someclass();`) or call a static method, it should be pretty easy with the standard reflection libraries. – StriplingWarrior Jun 08 '21 at 15:32

1 Answers1

-1

This is what I was looking for.

using System;
namespace Test {
    public class Class1 {
        public int value = 1;
        public static void Main(string[] args) {
            var mInfo = typeof(Class1).GetMethod("Add");
            
            Class1 knownType = new Class1();
            object sortaUnknownType = new Class1();
            object anonType = new { value = 1 };
            
            // this works, obviously
            object v1 = mInfo.Invoke(knownType, new object[] {1});
            Console.WriteLine($"v1 = {v1}");

            // the type of sortaUnknownType is still Class1
            // .GetType() would show this
            object v2 = mInfo.Invoke(sortaUnknownType, new object[] {2});
            Console.WriteLine($"v2 = {v2}");

            // this crashes because it is genuinely not of type Class1
            // additionally, it cannot be cast to Class1 because
            // its of type {int value} as the comments mentioned
            object v3 = mInfo.Invoke(anonType, new object[] {3});
            Console.WriteLine($"v3 = {v3}");
        }

        // this is the method i want to invoke
        public int Add(int input) {
            return this.value + input;
        }
    }
}

This is the method I mentioned in my comment: void _MethodInfo.Invoke (uint dispIdMember, ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr);

VeryGoodDog
  • 335
  • 2
  • 11