代码之家  ›  专栏  ›  技术社区  ›  Coppermill

属性类不调用构造函数

  •  5
  • Coppermill  · 技术社区  · 15 年前

    我已经创建了一个属性call myattribute,它正在执行一些安全性,出于某种原因,构造函数没有被激发,有什么原因吗?

    public class Driver
    {
        // Entry point of the program
        public static void Main(string[] Args)
        {
            Console.WriteLine(SayHello1("Hello to Me 1"));
            Console.WriteLine(SayHello2("Hello to Me 2"));
    
            Console.ReadLine();
        }
    
        [MyAttribute("hello")]
        public static string SayHello1(string str)
        {
            return str;
        }
    
        [MyAttribute("Wrong Key, should fail")]
        public static string SayHello2(string str)
        {
            return str;
        }
    
    
    }
    
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
    
        public MyAttribute(string VRegKey)
        {
            if (VRegKey == "hello")
            {
                Console.WriteLine("Aha! You're Registered");
            }
            else
            {
                throw new Exception("Oho! You're not Registered");
            };
        }
    }
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Andrew Bezzub    15 年前

    实际上它失败了,但只有当您试图获取属性属性时。下面是一个失败的例子:

    using System;
    
    public class Driver
    {
    // Entry point of the program
        public static void Main(string[] Args)
        {
            Console.WriteLine(SayHello1("Hello to Me 1"));
            Console.WriteLine(SayHello2("Hello to Me 2"));
    
            Func<string, string> action1 = SayHello1;
            Func<string, string> action2 = SayHello2;
    
            MyAttribute myAttribute1 = (MyAttribute)Attribute.GetCustomAttribute(action1.Method, typeof(MyAttribute));
            MyAttribute myAttribute2 = (MyAttribute)Attribute.GetCustomAttribute(action2.Method, typeof(MyAttribute));
    
            Console.ReadLine();
        }
    
        [MyAttribute("hello")]
        public static string SayHello1(string str)
        {
            return str;
        }
    
        [MyAttribute("Wrong Key, should fail")]
        public static string SayHello2(string str)
        {
            return str;
        }
    
    
    }
    
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
    
        public string MyProperty
        {
            get; set;
        }
    
        public string MyProperty2
        {
            get;
            set;
        }
    
        public MyAttribute(string VRegKey)
        {
            MyProperty = VRegKey;
            if (VRegKey == "hello")
            {
                Console.WriteLine("Aha! You're Registered");
            }
            else
            {
                throw new Exception("Oho! You're not Registered");
            };
    
            MyProperty2 = VRegKey;
        }
    }
    
        2
  •  8
  •   Dave Van den Eynde    15 年前

    属性在编译时应用,构造函数仅用于填充属性。属性是元数据,只能在运行时检查。

    事实上,属性根本不应该包含任何行为。