It often happens (at least in the AutoFixture code base) that you have an object that may be some Reflection object, like PropertyInfo, or ParameterInfo, but you don't know which.
The whole point of Albedo is to provide a polymorphic API over Reflection, so it'd be nice to be able to automatically translate from object to IReflectionElement.
Something like
var re = obj.AsReflectionElement()
However, as far as I can tell, there are at least two different scenarios:
The provided objectmust be convertible into one of the known, proper IReflectionElement implementations, so if it's not a PropertyInfo, ParameterInfo, etc. an exception should be thrown.
The provided object may not be a Reflection instance (like PropertyInfo, ParameterInfo, etc.). This is expected, in which case a NullReflectionElement (see #23) must be returned.
Perhaps there are more scenarios? This indicates to me that we should implement this as a proper extensible Factory class.
Something like this should do the trick:
public interface IReflectionElementPipe<T>
{
IEnumerable<IReflectionElement> Pipe(IEnumerable<T> source);
}
(I'm not quite sure about the name, so suggestions are welcome...)
While T would often be object, the generic type argument exists to support scenarios where you know that you can only receive objects that implement IMemberInfo, in which case you can declare the T as IMemberInfo...
Now you can implement an individual pipe like:
public class PropertyInfoElementPipe<T> : IReflectionElementPipe<T>
{
public IEnumerable<IReflectionElement> Pipe(IEnumerable<T> source)
{
return source
.OfType<PropertyInfo>()
.Select(pi => new PropertyInfoElement(pi));
}
}
Given all these, you could implement the actual factory as a composite of all those...
It often happens (at least in the AutoFixture code base) that you have an
object
that may be some Reflection object, likePropertyInfo
, orParameterInfo
, but you don't know which.The whole point of Albedo is to provide a polymorphic API over Reflection, so it'd be nice to be able to automatically translate from
object
toIReflectionElement
.Something like
However, as far as I can tell, there are at least two different scenarios:
object
must be convertible into one of the known, properIReflectionElement
implementations, so if it's not aPropertyInfo
,ParameterInfo
, etc. an exception should be thrown.object
may not be a Reflection instance (likePropertyInfo
,ParameterInfo
, etc.). This is expected, in which case aNullReflectionElement
(see #23) must be returned.Perhaps there are more scenarios? This indicates to me that we should implement this as a proper extensible Factory class.
Something like this should do the trick:
(I'm not quite sure about the name, so suggestions are welcome...)
While
T
would often beobject
, the generic type argument exists to support scenarios where you know that you can only receive objects that implementIMemberInfo
, in which case you can declare theT
asIMemberInfo
...Now you can implement an individual pipe like:
Given all these, you could implement the actual factory as a composite of all those...