自定义Attribute 的疑惑

时间:2023-01-29 12:32:25
自己定义了了个Attribute 其中一个属性是自己定义的借口IFotmatStr 自定义的Attribute大概如下
public class ColumnAttribute:Attribute
{
private Type _formatHelper=null;

        /// <summary>
/// 格式化类
/// </summary>
public IFotmatStr FormatHelper
{
get{return _formatHelper;}
set{_formatHelper=value;}
}
}
在属性使用的时候
[Column(FormatHelper=new FotmatStr())]
FotmatStr()继承IFotmatStr
编译报错了信息
“FormatHelper”不是有效的属性实参,因为它不是有效的命名属性形参类型
大家给我卡看是怎么回事啊

15 个解决方案

#1


不懂,帮顶

#2




public class ColumnAttribute:Attribute 


public ColumnAttribute(Type t)
{
   this.formatHelper=t;
}
private Type _formatHelper=null; 

      /// <summary> 
/// 格式化类 
/// </summary> 
public IFotmatStr FormatHelper 

get{return _formatHelper;} 
set{_formatHelper=value;} 


在属性使用的时候 
[Column(typeof(string))] 
public ....
FotmatStr()继承IFotmatStr 


#3


你的 _formatHelper 不能定义成type类型把。你试下定义成IFotmastStr 试下。

#4


[Column(tyoeof(FormatHelper))] 

#5


public class ColumnAttribute:Attribute 

private IFotmatStr _formatHelper=null; 

      /// <summary> 
/// 格式化类 
/// </summary> 
public IFotmatStr FormatHelper 

get{return _formatHelper;} 
set{_formatHelper=value;} 



我这个是这样的 刚才打错了 问题还存在

#6


ColumnAttribute编译的时候是正确的
但是ColumnAttribute使用的时候 给FormatHelper属性赋值的时候出错

#7




public class ColumnAttribute:Attribute 


public ColumnAttribute(IFotmatStr t)
{
   this.formatHelper=t;
}
private Type _formatHelper=null; 

      /// <summary> 
/// 格式化类 
/// </summary> 
public IFotmatStr FormatHelper 

get{return _formatHelper;} 
set{_formatHelper=value;} 


在属性使用的时候 
[Column( new FotmatStr()] 
public ....
FotmatStr()继承IFotmatStr 

#8


 7 楼 ojekleen 首先感谢你,不过还是出错
错误 1 属性实参必须是属性形参类型的常量表达式、typeof 表达式或数组创建表达式

而且你是在构造函数复制,我是给属性赋值,应该是一个结果的,继续等高手

#9


学习一下

#10


调用的时用
[Column( new FotmatStr())]试试。一开始没看清楚你的代码。
 

#11


试了 还是出错的。。。

#12


试了下代码。
属性参数必须是常量表达式、typeof 表达式或数组创建表达式
你这样属于变量,的确不行。

#13


那应该怎么该?

#14


属性实参必须是属性形参类型的常量表达式、typeof 表达式或数组创建表达式,
已经说的很清楚了

#15


我今天也遇到了这问题.
一个nullable 的枚举也不行.

public sealed class SqlColumnMapAttribute:Attribute{
    public string ColumnName{get;set;}
    public SqlDbType DbType{get;set;}
    //public SqlDbType? DbType{get;set;} 这个情况下,就出"不是有效的属性实参,因为它不是有效的命名属性形参类型"
}

#1


不懂,帮顶

#2




public class ColumnAttribute:Attribute 


public ColumnAttribute(Type t)
{
   this.formatHelper=t;
}
private Type _formatHelper=null; 

      /// <summary> 
/// 格式化类 
/// </summary> 
public IFotmatStr FormatHelper 

get{return _formatHelper;} 
set{_formatHelper=value;} 


在属性使用的时候 
[Column(typeof(string))] 
public ....
FotmatStr()继承IFotmatStr 


#3


你的 _formatHelper 不能定义成type类型把。你试下定义成IFotmastStr 试下。

#4


[Column(tyoeof(FormatHelper))] 

#5


public class ColumnAttribute:Attribute 

private IFotmatStr _formatHelper=null; 

      /// <summary> 
/// 格式化类 
/// </summary> 
public IFotmatStr FormatHelper 

get{return _formatHelper;} 
set{_formatHelper=value;} 



我这个是这样的 刚才打错了 问题还存在

#6


ColumnAttribute编译的时候是正确的
但是ColumnAttribute使用的时候 给FormatHelper属性赋值的时候出错

#7




public class ColumnAttribute:Attribute 


public ColumnAttribute(IFotmatStr t)
{
   this.formatHelper=t;
}
private Type _formatHelper=null; 

      /// <summary> 
/// 格式化类 
/// </summary> 
public IFotmatStr FormatHelper 

get{return _formatHelper;} 
set{_formatHelper=value;} 


在属性使用的时候 
[Column( new FotmatStr()] 
public ....
FotmatStr()继承IFotmatStr 

#8


 7 楼 ojekleen 首先感谢你,不过还是出错
错误 1 属性实参必须是属性形参类型的常量表达式、typeof 表达式或数组创建表达式

而且你是在构造函数复制,我是给属性赋值,应该是一个结果的,继续等高手

#9


学习一下

#10


调用的时用
[Column( new FotmatStr())]试试。一开始没看清楚你的代码。
 

#11


试了 还是出错的。。。

#12


试了下代码。
属性参数必须是常量表达式、typeof 表达式或数组创建表达式
你这样属于变量,的确不行。

#13


那应该怎么该?

#14


属性实参必须是属性形参类型的常量表达式、typeof 表达式或数组创建表达式,
已经说的很清楚了

#15


我今天也遇到了这问题.
一个nullable 的枚举也不行.

public sealed class SqlColumnMapAttribute:Attribute{
    public string ColumnName{get;set;}
    public SqlDbType DbType{get;set;}
    //public SqlDbType? DbType{get;set;} 这个情况下,就出"不是有效的属性实参,因为它不是有效的命名属性形参类型"
}