garrettb12
garrettb12

Reputation: 11

C# Reflection/NUnit - A Func living in a public field of object instance is private. Why? How do I wrap it to make it public?

What I'm trying to achieve/Why: I am trying to extend NUnit to recognize Func delegates in a particular class type's instance as unit tests. I am using a custom attribute that implements IFixtureBuilder - which is an interface that you use when you wish to extrapolate an NUnit TestMethod from something other than a method decorated with [Test].

I am able to get a reference to the Func(s) and their MethodInfo(s), but NUnit will only allow me to create a TestMethod from MethodInfo objects where IsPublic == true. Consider the example sketched up here:

https://dotnetfiddle.net/JHNBm6

In my example above, the variable 'firstMethod's IsPublic property is false. For me to recognize this function as an NUnit TestMethod, it needs to be public.

Why is this Func delegate considered private? My Lists are in a public field on the TestClass instance. Is there a way to "wrap" an invocation of these funcs via System.Reflection trickery so that I have a MethodInfo that is public?

Upvotes: 1

Views: 144

Answers (1)

Shah
Shah

Reputation: 1399

Its returning false as you are adding anonymous function to List. Try adding below and you will get IsPublic == true

public class TestClass
{
    public List<Func<Task<bool>>> tests = new List<Func<Task<bool>>>();

    public TestClass()
    {
        tests.Add(TestMethod1);
    }

    public Task<bool> TestMethod1()
    {
        return Task.FromResult(true);
    }

}

Upvotes: 1

Related Questions