using System;
namespace ClassLibrary1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public enum RemoteServers
{
JEANVALJEAN,
JAVERT,
COSETTE
}
public class RemoteObjectAttribute:Attribute
{
public RemoteObjectAttribute(RemoteServers Server)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.server=Server;
}
protected RemoteServers server;
public string Server
{
get
{
return RemoteServers.GetName(typeof(RemoteServers),this.server);
}
}
[RemoteObject(RemoteServers.COSETTE)]
class MyRemotableClass
{
}
}
}
using System;
using ClassLibrary1;
namespace CsharpClassAttribute
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Type type=typeof(MyRemotableClass);
foreach(Attribute attr in type.GetCustomAttributes(true))
{
RemoteObjectAttribute remoteAttr=attr as RemoteObjectAttribute;
if(null!=remoteAttr)
{
Console.WriteLine("Create this object on {0}",remoteAttr.Server);
}
}
}
}
}
这段代码没有调试出来,使用了类属性和 反射,摘自c#精髓,高手请指点

本文通过一个具体的示例介绍了如何在 C# 中使用类属性(Attributes)和反射(Reflection)。示例中定义了一个 RemoteObjectAttribute 属性,并将其应用于 MyRemotableClass 类上,然后通过反射获取该属性并读取其值。

被折叠的 条评论
为什么被折叠?



