Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use VS2010 PrivateObject to access a static field inside a static class?

Tags:

Is it possible to get access to a private static field inside a static class, using the VS2010 Unit Test class PrivateObject ?

Let say i have the following class:

public static class foo
{
    private static bar;
}

Can i use PrivateObject to create a copy of foo, and then get the bar field?

like image 410
Yiftach Tzur Avatar asked Mar 09 '11 16:03

Yiftach Tzur


People also ask

Can static class have constructor C#?

Static classes cannot contain an instance constructor. However, they can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.

Can we declare static variable in non-static class in C#?

A static class can only have static members — you cannot declare instance members (methods, variables, properties, etc.) in a static class. You can have a static constructor in a static class but you cannot have an instance constructor inside a static class.

Where do we use static class in C#?

The static modifier in C# declares a static member of a class. The static modifier can be used with classes, properties, methods, fields, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes.

What is a static variable C#?

Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.


1 Answers

PrivateType class is analogous to PrivateObject for invoking private static members. Overloaded GetStaticFieldOrProperty methods may be used. http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.privatetype(v=VS.100).aspx

like image 75
Deepum Avatar answered Sep 17 '22 12:09

Deepum