none
How to execute a string as code using Roslyn RRS feed

  • Question

  • Here is what I tried:

    using System; using System.Threading.Tasks; using static HMSan.Game; using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.Scripting; using System.Reflection; static void Trait_0(double[] Modifiers) { //Do something } static void Trait_1(double[] Modifiers) { //Do something } for (int i = 0; i < 2; i++) { string myscript = "HMSan.Game.TraitEffects[" + i + "] = HMSan.Trait.Trait_" + i + ";"; await CSharpScript.RunAsync(@myscript, ScriptOptions.Default.WithReferences(Assembly.GetExecutingAssembly())); }

    "TraitEffects" is a list of delegates. I am trying to loop through the names of a series of methods and put them in "TraitEffects". The problem is that the compiler said I did not have the methods defined yet. What should I do to dynamically get references to these methods?

    Saturday, October 17, 2020 8:48 PM

Answers

  • Hi antidinosaur,

    Thank you for posting here.

    Is it possible to use reflection to bind delegates and methods?

    The speed of reflection may be slightly slower.

            static void Trait_0(string message)
            {
                Console.WriteLine(message+"0");
            }
            static void Trait_1(string message)
            {
                Console.WriteLine(message+"1");
            }
            public delegate void Del(string message);
            static void Main(string[] args)
            {
                Del[] dels = new Del[2];
                for (int i = 0; i < 2; i++)
                {
                    MethodInfo methodInfo = typeof(Program).GetMethod("Trait_" + i, BindingFlags.NonPublic | BindingFlags.Static);
                    dels[i] = (Del)Del.CreateDelegate(typeof(Del), methodInfo);
                }
            }

    If this does not meet your requirements, I'm afraid you'd better ask your question in Roslyn's GitHub repository.

    Many members here may not have experience with Roslyn.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by antidinosaur Monday, October 19, 2020 5:21 PM
    Monday, October 19, 2020 6:22 AM

All replies

  • Hi antidinosaur,

    Thank you for posting here.

    Is it possible to use reflection to bind delegates and methods?

    The speed of reflection may be slightly slower.

            static void Trait_0(string message)
            {
                Console.WriteLine(message+"0");
            }
            static void Trait_1(string message)
            {
                Console.WriteLine(message+"1");
            }
            public delegate void Del(string message);
            static void Main(string[] args)
            {
                Del[] dels = new Del[2];
                for (int i = 0; i < 2; i++)
                {
                    MethodInfo methodInfo = typeof(Program).GetMethod("Trait_" + i, BindingFlags.NonPublic | BindingFlags.Static);
                    dels[i] = (Del)Del.CreateDelegate(typeof(Del), methodInfo);
                }
            }

    If this does not meet your requirements, I'm afraid you'd better ask your question in Roslyn's GitHub repository.

    Many members here may not have experience with Roslyn.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by antidinosaur Monday, October 19, 2020 5:21 PM
    Monday, October 19, 2020 6:22 AM
  • Thank you very much Timon!

    This is what I am looking for.  Only one thing I need it to be clarified:

    In your example code: 

    MethodInfo methodInfo = typeof(Program).GetMethod("Trait_" + i, BindingFlags.NonPublic |BindingFlags.Static);

    I should replace "Progam" with the name of the class where my Trait methods located,right? 

    Monday, October 19, 2020 5:19 PM
  • Yes, it is.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 20, 2020 1:11 AM