Update 2011-07-12: Refer to this answer on Stackoverflow for a more elegant solution.
I've seen many questions about how to create an instance of this class in online forums when I searched for it myself today. The solution is to delve into the internals of the class with Reflection. In that way it is possible to call the two private constructors of the class.
You can use the code below:
using System.Reflection;
using System.Web.UI;
namespace Tools
{
public class CssStyleTools
{
/// <summary>
/// Creates an instance of the CssStyleCollection class by reflection.
/// </summary>
/// <returns>A new instance of the CssStyleCollection class.</returns>
public static CssStyleCollection Create()
{
return (CssStyleCollection)typeof(CssStyleCollection).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0].Invoke(null);
}
/// <summary>
/// Creates an instance of the CssStyleCollection class by reflection and stores view state in the provided state bag.
/// </summary>
/// <param name="state">The storage object for the state.</param>
/// <returns>A new instance of the CssStyleCollection class.</returns>
public static CssStyleCollection Create(StateBag state)
{
return (CssStyleCollection)typeof(CssStyleCollection).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[1].Invoke( new object[]{ state });
}
}
}
It is as simple as calling the following code:
CssStyleCollection css = CssStyleTools.Create();
Feel free to comment if it was any use to you.