制定 “特性” 的问题,为什么 “特性” 没有发生呢?           {初学帖}

noflyzone 2009-03-24 11:44:30

[TestAttribute("1测试Attribute代码")] //不是说当运行到这个TAtrributes类时,编译器就会查找TestAttribute特性吗?并实例化TestAttribute特性类,可是我在TestAttribute特性类中的构造中写的Console.Write()没执行哦?
class TAtrributes
{

public TAtrributes()
{
Console.WriteLine("2这是Tatrribute类");
}
}


[System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class TestAttribute : Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
readonly string positionalString;

// This is a positional argument
public TestAttribute(string positionalString)
{
this.positionalString = positionalString;

// TODO: Implement code here
Console.WriteLine(positionalString);
}

public string PositionalString { get; private set; }

// This is a named argument
public int NamedInt { get; set; }
}

TAtrributes i = new TAtrributes();

输出结果为:2这是Tatrribute类     我以为应该是:先是输出 1测试Attribute代码,然后是 2这是Tatrribute类,为什么没出现我说的这种情况呢?
...全文
102 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzxap 2009-03-24
  • 打赏
  • 举报
回复
Type t=typeof(tattributs);
t.GetCustomAttributes();

TAtrributes i = new TAtrributes();
ojekleen 2009-03-24
  • 打赏
  • 举报
回复
其实在反射的时候,也不见new 特性类才可以使用,特性类的确在使用该特性的类中自动实例化的.
只是楼主 以为是前置加载,即在使用类实例化就会先实例化该类所有使用的特性类.
而.net很可能是后置加载,即在类使用到特性的时候再实例化使用到的特性类.即也所谓的延迟加载.

楼主的误区并不远

可以加上

Type t=typeof(tattributs);
t.GetCustomAttributes();

TAtrributes i = new TAtrributes();

这样应该可以实现楼主的输出效果.

//因为没有编译器,以上结果仅为猜测,楼主可以试试.如有错误,请谅解.
heyu52 2009-03-24
  • 打赏
  • 举报
回复
同意11楼的说法
wanhot 2009-03-24
  • 打赏
  • 举报
回复
制定特性只是把定制的元数据与程序元素关联起来,也没有楼主说的编译器到那个类就先实例化特性类。其实特性是为反射作准备的。
heyu52 2009-03-24
  • 打赏
  • 举报
回复
private void OpenFunctionForm(FormBase functionForm)
{
try
{
functionForm.MdiParent = this;
functionForm.Show();
Application.DoEvents();
functionForm.Activate();
}
catch (Exception ex)
{
MessageBox.Show("打开功能失败" + "\n" + ex.Message);
}
}
heyu52 2009-03-24
  • 打赏
  • 举报
回复
public void OpenFunctionForm(string functionCode)
{
functionCode = functionCode.ToUpper().Trim();
Type formBaseType = null;

if (!_formBaseType.TryGetValue(functionCode, out formBaseType))
{
string[] arFile = Directory.GetFiles(Application.StartupPath, "HVS.LookUp.dll");
for (int i = 0; i < arFile.Length; i++)
{
Assembly assembly = Assembly.LoadFrom(arFile[i]);
foreach (Type type in assembly.GetTypes())
{
try
{
object[] attributes = type.GetCustomAttributes(typeof(FunctionCode), true);
foreach (object obj in attributes)
{
FunctionCode attribute = (FunctionCode)obj;
if (!string.IsNullOrEmpty(attribute.Value))
{
if (!_formBaseType.ContainsKey(attribute.Value))
_formBaseType.Add(attribute.Value, type);

if (formBaseType == null && string.CompareOrdinal(attribute.Value, functionCode) == 0)
formBaseType = type;
}

if (formBaseType != null)
break;
}

}
catch
{

}
}
}
}

if (formBaseType != null)
{
object entryForm = Activator.CreateInstance(formBaseType) as FormBase;
FormBase functionForm = (FormBase)entryForm;
OpenFunctionForm(functionForm);
}

}
heyu52 2009-03-24
  • 打赏
  • 举报
回复
我给你一个例子(自己写的,不是抄别的,不要误会)

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public sealed class FunctionCode : Attribute
{
private string _value;

public string Value
{
get
{
return _value;
}
}

public FunctionCode(string value)
{
_value = value;
}
}
chwlyr 2009-03-24
  • 打赏
  • 举报
回复
我贴出msdn的说明,是想告诉你Attribute到底是怎么一回事。

从你的测试代码的用法,可以看出你根本不知道什么是Attribute,所以才贴了msdn的原话。
chwlyr 2009-03-24
  • 打赏
  • 举报
回复
可以使用Assembly.GetCustomAttributes 方法
获取按类型指定的此程序集的自定义属性。
noflyzone 2009-03-24
  • 打赏
  • 举报
回复
我说的是一楼的兄弟,与二楼三楼无关
noflyzone 2009-03-24
  • 打赏
  • 举报
回复
楼上的兄弟,不是所有在CSDN上的问问题的人都散分的,象这类从MSDN上Copy的,你可以免了,一分也不给你,就算给你个钻石,你???快乐否??!!
chwlyr 2009-03-24
  • 打赏
  • 举报
回复
要输出:1测试Attribute代码

需要使用反射来实现


ojekleen 2009-03-24
  • 打赏
  • 举报
回复
如果没有 的话,哪应该就不是你说的这样的喽,
可能是特性集合延迟加载的方式 吧,应该是你调用特性的时候再实例化特性类.
chwlyr 2009-03-24
  • 打赏
  • 举报
回复
特性:(Attribute) 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。

属性所提供的信息也称为元数据。元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。例如,.NET Framework 预定义属性类型并使用属性类型控制运行时行为,某些编程语言使用属性类型表示 .NET Framework 公共类型系统不直接支持的语言功能。

所有属性类型都直接或间接地从 Attribute 类派生。属性可应用于任何目标元素;多个属性可应用于同一目标元素;并且属性可由从目标元素派生的元素继承。使用 AttributeTargets 类可以指定属性所应用到的目标元素。

Attribute 类提供检索和测试自定义属性的简便方法.

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧