Exemplo n.º 1
0
        public void UsesEntryPointFormat()
        {
            // Arrange
            var type    = module.DefineType("ConstructorType_UsesEntryPointFormat", TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.AutoLayout | TypeAttributes.Public);
            var builder = new DefaultConstructorBuilder(null);
            var lib     = Substitute.For <ILibrary>();

            lib.GetProcedure <Func <string> >(Arg.Any <string>()).Returns(() => "Hello world!");

            var f = type.DefineField("_Foo_", typeof(Func <string>), FieldAttributes.Private | FieldAttributes.InitOnly);

            // Act
            var ctor   = builder.GenerateConstructor(type, typeof(IFooWithNamingConvention), typeof(IFooWithNamingConvention).GetMethods(), new[] { f });
            var result = Activator.CreateInstance(type.CreateType(), lib);

            // Assert
            lib.Received().GetProcedure <Func <string> >("prefix-Foo");
        }
        public void GeneratesConstructor_Ok()
        {
            // Arrange
            var type = module.DefineType("ConstructorType_Ok", TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.AutoLayout | TypeAttributes.Public);
            var builder = new DefaultConstructorBuilder(null);
            var lib = Substitute.For<ILibrary>();
            lib.GetProcedure<Func<string>>("Foo").Returns(() => "Hello world!");

            var f = type.DefineField("_Foo_", typeof(Func<string>), FieldAttributes.Private | FieldAttributes.InitOnly);

            // Act
            var ctor = builder.GenerateConstructor(type, typeof(IFoo), typeof(IFoo).GetMethods(), new[] { f });

            // Assert
            var result = Activator.CreateInstance(type.CreateType(), lib);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.GetType().GetField("_Foo_", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(result));
        }
Exemplo n.º 3
0
        public void GeneratesConstructor_Ok()
        {
            // Arrange
            var type    = module.DefineType("ConstructorType_Ok", TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.AutoLayout | TypeAttributes.Public);
            var builder = new DefaultConstructorBuilder(null);
            var lib     = Substitute.For <ILibrary>();

            lib.GetProcedure <Func <string> >("Foo").Returns(() => "Hello world!");


            var f = type.DefineField("_Foo_", typeof(Func <string>), FieldAttributes.Private | FieldAttributes.InitOnly);

            // Act
            var ctor = builder.GenerateConstructor(type, typeof(IFoo), typeof(IFoo).GetMethods(), new[] { f });

            // Assert
            var result = Activator.CreateInstance(type.CreateType(), lib);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.GetType().GetField("_Foo_", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(result));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a default constructor
 /// </summary>
 /// <param name="Attributes">Attributes for the constructor (public, private, etc.)</param>
 /// <returns>Constructor builder for the constructor</returns>
 public virtual IMethodBuilder CreateDefaultConstructor(MethodAttributes Attributes = MethodAttributes.Public)
 {
     DefaultConstructorBuilder ReturnValue = new DefaultConstructorBuilder(this, Attributes);
     Constructors.Add(ReturnValue);
     return ReturnValue;
 }
        public void UsesEntryPointFormat()
        {
            // Arrange
            var type = module.DefineType("ConstructorType_UsesEntryPointFormat", TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.AutoLayout | TypeAttributes.Public);
            var builder = new DefaultConstructorBuilder(null);
            var lib = Substitute.For<ILibrary>();
            lib.GetProcedure<Func<string>>(Arg.Any<string>()).Returns(() => "Hello world!");

            var f = type.DefineField("_Foo_", typeof(Func<string>), FieldAttributes.Private | FieldAttributes.InitOnly);

            // Act
            var ctor = builder.GenerateConstructor(type, typeof(IFooWithNamingConvention), typeof(IFooWithNamingConvention).GetMethods(), new[] { f });
            var result = Activator.CreateInstance(type.CreateType(), lib);

            // Assert
            lib.Received().GetProcedure<Func<string>>("prefix-Foo");
        }