[AttributeUsage(AttributeTargets.Class,Inherited=true,AllowMultiple=false)]
public class TestAttribute:Attribute
{
private string _author;
public TestAttribute(string author)
{
_author = author;
}
public bool CheckKey()
{
if (this._author.StartsWith("A") && this._author.EndsWith("D"))
return true;
else
return false;
}
}
[Serializable]
[Test("ABCD")]
public class Fruit
{
[RegularExpression("(a-z){1,5}$",ErrorMessage="种类错误")]
public string FruitKind;
[Range(1,100,ErrorMessage="价格离谱")]
public int price;
} 那么在实例Fruit中,应该怎么应用这些属性呢?
class Program
{
static void Main(string[] args)
{
Fruit f = new Fruit();
f.FruitKind = "apple";
f.price = 10;
Type type = f.GetType();
//1、怎么反射并判断利用Test的CheckKey属性呢?
//2、怎么判断f的FruitKind、price是否违反类中的属性定义?(类似mvc中的ModelState.IsValid方式)
}
}
解决方案 »
- winform里如何遍历获取treeview的选中状态?
- DataGridView中一次显示两个表中的数据
- serialPort串口通讯
- 请问如何能读取一个TXT文件?
- 兄弟们进来看看 帮改个错误
- 请问如何使用socket传送多个文件(一个文件我会做,三个以上不会),请附源码!!!
- 开学就大四了,学C#半年了,学校的课却没有学,大四该继续学C#吗?
- 哪位大侠有用mdi多窗体做的c#程序,让小弟我学习一下!先送一百分,有收获的话,分再狂送!
- .net太多了,我们具体应该学哪一种比较好,是c#.net还是vc.net,请帮助
- 怎么将excel中数据用C#读取并存在一个数组中?
- C# WinForm程序 客户自定义界面 ! 求助 谢谢
- 关于C#引用其他文件的小白问题 请高手帮忙
foreach (PropertyInfo tmp in type.GetProperties())
{
object[] ossa = tmp.GetCustomAttributes(typeof(attribute), true);
if (ossa.Length > 0)
{
return ((OSSAttribute)ossa[0]).AccessMode;
}
}