Search code examples
c#lazy-initialization.net-4.7

Determine if a variable is of a lazy type


Quick question - is there any 'prettier' way to determine if a variable was lazily initialized than:

var paramType = param.GetType();
if (paramType.FullName.Contains("System.Lazy")) 
{
    ...
}

I know it's not something really recommended to be checked, but there is a particular need to differentiate it.


Solution

  • Try this:

    if (paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof(Lazy<>))
    

    It should return true if param is a Lazy<T> (and paramType is param.GetType()).