Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Delegate.Method fails when called in constructor. #1051

Open
baehny opened this issue Dec 9, 2017 · 3 comments
Open

Delegate.Method fails when called in constructor. #1051

baehny opened this issue Dec 9, 2017 · 3 comments

Comments

@baehny
Copy link

baehny commented Dec 9, 2017

Delegate.Method crashs when called in a constructor. The same code works when called inside a member method that is called in the ctor.

    class Class1
    {
        public Class1()
        {
            Delegate d = new Action<int>(
            (int someVar) =>
            {

            });
            var p = d.Method.GetParameters(); // TypeError: d.get_Method(...) is null
        }
    }
    class Class2
    {
        public Class2()
        {
            MemberMethod();
        }

        public void MemberMethod()
        {
            Delegate d = new Action<int>(
            (int someVar) =>
            {

            });
            var p = d.Method.GetParameters(); // TypeError: d.get_Method(...) is null
        }
    }

    public class JSILLimitations
    {
        public static void DelegateBug()
        {
            Delegate d = new Action<int>(
            (int console) =>
            {

            });
            var p = d.Method.GetParameters();
            Console.WriteLine("Static method version works.");

            Class2 clazz2 = new Class2();
            Console.WriteLine("Member method call version works.");

            Class1 clazz1 = new Class1();
            //Crash
            Console.WriteLine("Ctor method version works."); //
        }
    }
@garata
Copy link

garata commented Dec 21, 2017

@baehny I have given a try to reproduce your issue using "Try JSIL" about delegate called in a constructor.

http://jsil.org/try/#150e92605a2cfcfdaa5acea34a93f404

While your context may be different, could you kindly tell me if my test case is different from your example?

It seems that a delegate called in constructor placed into Main mathod does not crash.

@baehny
Copy link
Author

baehny commented Jan 23, 2018

Your example looks very similar. If I test my setup with "Try JSIL" it works too so it must be some side effect of my project. I'm pretty sure that "Try JSIL" uses .Net Framework 4.5 or lower while my project uses 4.7.1. I have to do further investigation if I have some time but sadly I'm quite busy right now.

@baehny
Copy link
Author

baehny commented Jan 26, 2018

I have uploaded a test project that recreates the issue with a minimal setup.
The project targets .Net Framework 4.5

https://github.com/baehny/Bugs/tree/master/JSIL/JSIL_DelegateMethodInCtor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants