public ReplacementStaticFieldAssignmentBuilder(ReplacementMethodConstructionContext context, ReplaceableFieldInfo fieldInfo)
     : base(context)
 {
     var reflector = new UnitReflector(context.Host);
     var containingType = reflector.Get(fieldInfo.DeclaringType.FullName);
     field = reflector.From(containingType).GetField(fieldInfo.Name);
 }
        private void AddInterceptionTargets()
        {
            host.LoadUnit(host.CoreAssemblySymbolicIdentity);

            foreach (var method in registry.GetRegisteredReferences(ReplaceableReferenceTypes.Method))
            {
                var methodInfo = method as ReplaceableMethodInfo;

                var fullNamespace = methodInfo.DeclaringType.Namespace;
                var fullNamespaceWithType = methodInfo.DeclaringType.FullName;

                log.WriteTrace("Adding interception target for '{0}'.", fullNamespaceWithType);

                fakeNamespace.AddNamespaces(fullNamespace);
                fakeNamespace.AddClass(fullNamespace, methodInfo.DeclaringType.Name);

                var reflector = new UnitReflector(host);
                var methodType = reflector.Get(methodInfo.ReturnType.FullName);

                var actualMethod = methodInfo.Name == ".ctor" ?
                    reflector.From(fullNamespaceWithType).GetConstructor(methodInfo.Parameters.Select(p => reflector.Get(p.ParameterType.FullName)).ToArray()) :
                    reflector.From(fullNamespaceWithType).GetMethod(methodInfo.Name, methodInfo.Parameters.Select(p => reflector.Get(p.ParameterType.FullName)).ToArray());

                var methodClass = fakeNamespace.Classes[fullNamespaceWithType];
                var methodName = methodInfo.Name == ".ctor" ? "<constructor>" : methodInfo.Name;
                var fakeMethod = methodClass.AddPublicStaticMethod(methodName, methodType, host);

                // if it's an instance method, we add a parameter at the end for the target
                ushort extraParameters = 0;
                if (!actualMethod.ResolvedMethod.IsStatic && !actualMethod.ResolvedMethod.IsConstructor)
                {
                    fakeMethod.AddParameter(0, "target", actualMethod.ContainingType, host, false, false);
                    extraParameters = 1;
                }

                foreach (var parameter in actualMethod.Parameters)
                {
                    fakeMethod.AddParameter((ushort)(parameter.Index + extraParameters),
                        "p" + parameter.Index, parameter.Type, host, parameter.IsByReference/*IsOut*/, parameter.IsByReference);
                }

                if (actualMethod.ResolvedMethod.IsConstructor)
                {
                    fakeMethod.Type = actualMethod.ResolvedMethod.ContainingTypeDefinition;
                }

                var customAttribute = new CustomAttribute();
                customAttribute.Constructor = new UnitReflector(host)
                    .From<SharpMockGeneratedAttribute>().GetConstructor(Type.EmptyTypes);
                fakeMethod.Attributes = new List<ICustomAttribute>();
                fakeMethod.Attributes.Add(customAttribute);
                fakeMethod.Body = GetBody(fakeMethod, actualMethod);

                var parameterTypes = new List<ITypeDefinition>();
                foreach (var param in fakeMethod.Parameters)
                {
                    parameterTypes.Add(param.Type.ResolvedType);
                }

                var fakeCallReference = new Microsoft.Cci.MethodReference(host, fakeMethod.ContainingTypeDefinition,
                    fakeMethod.CallingConvention, fakeMethod.Type, fakeMethod.Name, 0, parameterTypes.ToArray());

                registry.RegisterReplacement(method, fakeCallReference);
            }

            foreach (var field in registry.GetRegisteredReferences(ReplaceableReferenceTypes.FieldAccessor))
            {
                var fieldReplacementBuilder = new FieldAccessorSourceWriter(fakeNamespace, host, log, field as ReplaceableFieldInfo);
                var fakeCallReference = fieldReplacementBuilder.GetReference();

                registry.RegisterReplacement(field, fakeCallReference);
            }

            foreach (var field in registry.GetRegisteredReferences(ReplaceableReferenceTypes.FieldAssignment))
            {
                var fieldReplacementBuilder = new FieldAssignmentSourceWriter(fakeNamespace, host, log, field as ReplaceableFieldInfo);
                var fakeCallReference = fieldReplacementBuilder.GetReference();

                registry.RegisterReplacement(field, fakeCallReference);
            }
        }