Exemple #1
0
        static void Main(string[] args)
        {
            var     type     = EmitAttributeBuilder.BuildTypeWithCustomAttributesOnMethod();
            dynamic instance = Activator.CreateInstance(type);

            instance.HelloWorld();
        }
Exemple #2
0
        public void EmitAttributeTest()
        {
            var myType = EmitAttributeBuilder.BuildTypeWithCustomAttributesOnMethod();

            object myInstance = Activator.CreateInstance(myType);

            object[] customAttrs = myType.GetCustomAttributes(true);

            _testOutputHelper.WriteLine("Custom Attributes for Type 'MyType':");

            object attrVal = null;

            foreach (object customAttr in customAttrs)
            {
                attrVal = typeof(ClassCreatorAttribute).InvokeMember("Creator",
                                                                     BindingFlags.GetProperty,
                                                                     null, customAttr, new object[] { });
                _testOutputHelper.WriteLine("-- Attribute: [{0} = \"{1}\"]", customAttr, attrVal);
            }

            _testOutputHelper.WriteLine("Custom Attributes for Method 'HelloWorld()' in 'MyType':");

            var num = myType.GetMember("HelloWorld");

            customAttrs = myType.GetMember("HelloWorld")[0].GetCustomAttributes(true);

            foreach (object customAttr in customAttrs)
            {
                attrVal = typeof(DateLastUpdatedAttribute).InvokeMember("DateUpdated",
                                                                        BindingFlags.GetProperty,
                                                                        null, customAttr, new object[] { });
                _testOutputHelper.WriteLine("-- Attribute: [{0} = \"{1}\"]", customAttr, attrVal);
            }

            _testOutputHelper.WriteLine("---");
        }