Example #1
0
        internal static T CreateLikenessResemblance <T>(Likeness <T> likeness)
        {
            var members = typeof(T)
                          .GetTypeInfo()
                          .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                          .Concat(typeof(T)
                                  .GetTypeInfo()
                                  .GetFields(BindingFlags.Public | BindingFlags.Instance)
                                  .Cast <MemberInfo>())
                          .ToArray();

            ProxyType proxyType =
                ProxyGenerator.FindCompatibleConstructor <T>(
                    likeness.Value,
                    members);

            TypeBuilder builder =
                ProxyGenerator.BuildType <T>(
                    ProxyGenerator.BuildModule(
                        ProxyGenerator.BuildAssembly(assemblyName)));

            FieldBuilder equals =
                ProxyGenerator.BuildFieldComparer(builder, likeness.GetType());

            ProxyGenerator.BuildConstructors <T>(
                proxyType.Constructor,
                builder,
                equals);

            ProxyGenerator.BuildMethodEquals(builder, equals);
            ProxyGenerator.BuildMethodGetHashCode <T>(builder);

            return((T)Activator.CreateInstance(
                       builder.CreateTypeInfo().AsType(),
                       proxyType.Parameters.Concat(
                           new[] { likeness }).ToArray()));
        }