Ejemplo n.º 1
0
        public Type CreateType()
        {
            if (FClass.IsCreated())
            {
                throw new InvalidOperationException(); // TODO: message
            }
            FEqualsGenerator.GenerateEpilogue();
            FGetHashCodeGenerator.GenerateEpilogue();

            return(FClass.CreateTypeInfo() !.AsType());
        }
Ejemplo n.º 2
0
        public void AddProperty(string name, Type type, params CustomAttributeBuilder[] customAttributes)
        {
            //
            // public XXX {}
            //

            PropertyBuilder property = FClass.DefineProperty(name, PropertyAttributes.None, type, Array.Empty <Type>());

            //
            // FXxX
            //

            FieldBuilder field = FClass.DefineField($"F{name}", type, FieldAttributes.Private);

            //
            // {get => FXxX;}
            //

            MethodBuilder getPropMthdBldr = FClass.DefineMethod(
                $"Get{name}",
                MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
                type,
                Type.EmptyTypes);

            ILGenerator ilGenerator = getPropMthdBldr.GetILGenerator();

            ilGenerator.Emit(Ldarg_0);
            ilGenerator.Emit(Ldfld, field);
            ilGenerator.Emit(Ret);

            //
            // {set => FXxX = value;}
            //

            MethodBuilder setPropMthdBldr = FClass.DefineMethod(
                $"Set{name}",
                MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
                null,
                new[] { type });

            ilGenerator = setPropMthdBldr.GetILGenerator();

            ilGenerator.Emit(Ldarg_0);
            ilGenerator.Emit(Ldarg_1);
            ilGenerator.Emit(Stfld, field);
            ilGenerator.Emit(Ret);

            //
            // public XXX {get; set; }
            //

            property.SetGetMethod(getPropMthdBldr);
            property.SetSetMethod(setPropMthdBldr);

            FGetHashCodeGenerator.ProcessProperty(property);
            FEqualsGenerator.ProcessProperty(property);

            foreach (CustomAttributeBuilder customAttribute in customAttributes)
            {
                property.SetCustomAttribute(customAttribute);
            }
        }