Export (0) Print
Expand All

PrivateObject.Invoke Method (String, BindingFlags, Type[], Object[], CultureInfo, Type[])

Used to access generic members of a private object.

Namespace:  Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

public Object Invoke(
	string name,
	BindingFlags bindingFlags,
	Type[] parameterTypes,
	Object[] args,
	CultureInfo culture,
	Type[] typeArguments
)

Parameters

name
Type: System.String

The name of the member to invoke.

bindingFlags
Type: System.Reflection.BindingFlags
parameterTypes
Type: System.Type[]

An array of Type objects that represents the number, order, and type of the parameters for the method to access.

-or-

An empty array of the type Type (that is, Type[] types = new Type[0]) to get a method that takes no parameters.

args
Type: System.Object[]

Any arguments that the member requires.

culture
Type: System.Globalization.CultureInfo

The CultureInfo object that represents the globalization locale to use, which might be necessary for locale-specific conversions, such as converting a numeric String to a Double.

-or-

null to use the current thread's CultureInfo.

typeArguments
Type: System.Type[]

An array of type arguments to use when invoking a generic method.

Return Value

Type: System.Object

The following code is in the application that you are testing. It contains a generic method that you want to test, TestThisMethod<T>.

internal class Customer
{
    internal T TestThisMethod<T>(T value)
    {
        return (value);
    }

    public Customer()
    {
    }
}

The following code is a test method in your unit test. It shows how to pass a type argument, in the final Type[] parameter, when it invokes the method that you are testing:

[TestMethod]
public void TestSetCustomerId()
{
    PrivateObject po = new PrivateObject(typeof(Customer));

    int id = 100;

    int actual = (int)po.Invoke("TestThisMethod", new Type[] { typeof(int) }, new Object[] { id }, new Type[] { typeof(int) });

    Assert.AreEqual(actual, id);
}

Show:
© 2014 Microsoft