Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: method 'is inaccessible due to its protection level'

0
0
Hi,

In the following short test when the script tries to access the method I get a ScriptEngineException:
"'ClearScriptAccessTest.TestClass.TestMethod()' is inaccessible due to its protection level"
However, accessing the property succeeds.
    class TestClass
    {
        public int TestProperty { get; set; }

        public void TestMethod()
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var engine = new V8ScriptEngine())
            {
                try
                {
                    TestClass testClass = new TestClass();
                    engine.AddHostObject("testClass", testClass);
                    engine.Execute(
                        @"
                    testClass.TestProperty = 17;
                    testClass.TestMethod();
"
                        );

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: '{0}'", ex.Message);
                }
            }

        }
    }
The obvious workaround is to change
class TestClass
to
public class TestClass
but in real life this requires changing many other classes.

I also tried adding
[assembly: InternalsVisibleTo("ClearScript")]
[assembly: InternalsVisibleTo("ClearScriptV8-64")]
[assembly: InternalsVisibleTo("ClearScriptV8-32")]
but that also didn't work.

Is there a reason why the property is accessible and the method is not?


Thanks a lot,
Ron

Viewing all articles
Browse latest Browse all 2297