属性类不是要求构造 [英] Attribute class not calling constructor
本文介绍了属性类不是要求构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了一个属性,调用MyAttribute,这是执行一些安全性和由于某些原因构造方法中没有被解雇,有什么理由?
公共类驱动程序
{
//程序的入口点
公共静态无效的主要(字串[] args)
{
Console.WriteLine(SayHello1(我打招呼1));
Console.WriteLine(SayHello2(我打招呼2)); 到Console.ReadLine();
} [MyAttribute(你好)]
公共静态字符串SayHello1(字符串str)
{
返回海峡;
} [MyAttribute(键错了,应该失败)]
公共静态字符串SayHello2(字符串str)
{
返回海峡;
}
}[AttributeUsage(AttributeTargets.Method)
公共类MyAttribute:属性
{ 公共MyAttribute(字符串VRegKey)
{
如果(VRegKey ==你好)
{
Console.WriteLine(啊哈您注册!);
}
其他
{
抛出新的异常(啊哈你不注册!);
};
}
}
解决方案
其实它失败了,但只有当你正试图获得属性的属性。这里是一个失败的例子:
使用系统;公共类驱动程序
{
//程序的入口点
公共静态无效的主要(字串[] args)
{
Console.WriteLine(SayHello1(我打招呼1));
Console.WriteLine(SayHello2(我打招呼2)); FUNC<字符串,字符串>动作1 = SayHello1;
FUNC<字符串,字符串>动作2 = SayHello2; MyAttribute myAttribute1 =(MyAttribute)Attribute.GetCustomAttribute(action1.Method的typeof(MyAttribute));
MyAttribute myAttribute2 =(MyAttribute)Attribute.GetCustomAttribute(action2.Method的typeof(MyAttribute)); 到Console.ReadLine();
} [MyAttribute(你好)]
公共静态字符串SayHello1(字符串str)
{
返回海峡;
} [MyAttribute(键错了,应该失败)]
公共静态字符串SayHello2(字符串str)
{
返回海峡;
}
}[AttributeUsage(AttributeTargets.Method)
公共类MyAttribute:属性
{ 公共myProperty的字符串
{
得到;组;
} 公共字符串MyProperty2
{
得到;
组;
} 公共MyAttribute(字符串VRegKey)
{
myProperty的= VRegKey;
如果(VRegKey ==你好)
{
Console.WriteLine(啊哈您注册!);
}
其他
{
抛出新的异常(啊哈你不注册!);
}; MyProperty2 = VRegKey;
}
}
I have created an Attribute, call MyAttribute, which is performing some security and for some reason the Constructor is not being fired, any reason why?
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");
};
}
}
解决方案
Actually it fails, but only if you are trying to get attribute properties. Here is an example that fails:
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;
}
}
这篇关于属性类不是要求构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文