Thursday, August 4, 2011

C# - Instantiate generic type parameters on class functions

protected T GetUserHeader<T>() where T : new()
{
    if (this.CustomHeader == null)
    {
        T userheader = new T();

        PropertyInfo appProperty = userheader.GetType().GetProperty("myProperty");
        if (appProperty.CanWrite)
             appProperty.SetValue(userheader, this.UserConfig.Application, null);
        this.CustomHeader = userheader;
    }
    return (T)CustomHeader;
}

No comments:

Post a Comment