I have a piece of code that attempts to cast and return an object called m_obj
depending on a given generic type. Here is the code:
private IObject m_obj;
public bool TryGetAs<T>(out T value) where T :IObject
{
value = m_obj as T; // The type parameter 'T' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint
// value = (T)m_obj; This compiles!
if (value != null)
return true;
return false;
}
I know that if m_obj
were a list, I could do this:
private readonly IList<Object> m_objects;
internal IList<T> Collect<T>() where T : IObject
{
return m_objects.OfType<T>().ToList();
}
However, I cannot figure out how to solve the problem in the first code example. Here is the original code again:
public interface IObject
{
}
This is my proposed solution:
public bool TryGetAs<T>(out T value) where T :IObject
{
value = default(T);
if (!(m_obj is T))
return false;
value = (T)m_obj;
return true;
}
I would like to know if my solution is correct and if there are any better or more elegant ways to solve this problem.