Mock friendly System.Reflection API, positive changes in .NET 4.0

Do not worry if this break does not look like an Reflection API

While testing my db_move_forward I realized that now you can completely mock Assembly class. Starting from .NET 4.0 most of the stuff is protected and virtual! For example, code below will create assembly representation with types supplied in argument tt. But as you may expect you can override virtually everything else. For example, assembly level attributes, resources, referenced assemblies. You name them :).

let createAssembly (tt : Type seq) =
    { new Assembly() with
          override x.GetTypes() = tt |> Seq.toArray }

And C# version is:

public class SuperAssembly {
	private readonly Type[] _types;
	public override Type[] GetTypes(){
		return _types;
	}
}

Nice feature if you have to test stuff that needs some specific assembly configuration. Isn’t it? Of course, Assembly is not the only member of Reflection API. Next candidate to be mocked is Type class. Well, nothing new here. But do not worry. Actually, Type class was extensible before .NET 4.0. I found TypeDelegator class to be the best for mocking. It implements all stuff by default. Code bellow is example of custom ‘Type’ implementation that overrides Name property.

let createType name =
	{ new TypeDelegator(typeof<string>) with
        override x.Name = name } :> Type

Next candidate is MemberInfo and Co. Actually, situation is the same as with Type. Code bellow mocks PropertyInfo class:

let createProperty n v =
	{ new PropertyInfo() with
			override x.Name = n
			member x.GetAccessors(b) = [||]
			member x.GetGetMethod(b) = failwith "Not implemented"
			member x.GetIndexParameters() = [||]
			member x.GetSetMethod(b) = failwith "Not implemented"
			member x.GetValue(o, f, b, i, c) = v
			member x.SetValue(o, v, f, b, i, c) = ()
			member x.Attributes = PropertyAttributes.None
			member x.CanRead = true
			member x.CanWrite = false
			member x.PropertyType = v.GetType()
			member x.DeclaringType = typeof<string>
			member x.ReflectedType = typeof<string>
			member x.GetCustomAttributes(b) = [||]
			member x.GetCustomAttributes(t, b) = [||]
			member x.IsDefined(t, b) = false }

As for me it less usable… bull well, this is better than nothing :).

Enjoy.

P.S. What do you think about F# examples? Actual code has been written with F#, but I am concerned about readability or even more understability. Let me know.