自己定义了了个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”不是有效的属性实参,因为它不是有效的命名属性形参类型
大家给我卡看是怎么回事啊
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”不是有效的属性实参,因为它不是有效的命名属性形参类型
大家给我卡看是怎么回事啊
{ 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
{
private IFotmatStr _formatHelper=null; /// <summary>
/// 格式化类
/// </summary>
public IFotmatStr FormatHelper
{
get{return _formatHelper;}
set{_formatHelper=value;}
}
} 我这个是这样的 刚才打错了 问题还存在
但是ColumnAttribute使用的时候 给FormatHelper属性赋值的时候出错
{ 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
错误 1 属性实参必须是属性形参类型的常量表达式、typeof 表达式或数组创建表达式 而且你是在构造函数复制,我是给属性赋值,应该是一个结果的,继续等高手
[Column( new FotmatStr())]试试。一开始没看清楚你的代码。
属性参数必须是常量表达式、typeof 表达式或数组创建表达式
你这样属于变量,的确不行。
已经说的很清楚了
一个nullable 的枚举也不行.public sealed class SqlColumnMapAttribute:Attribute{
public string ColumnName{get;set;}
public SqlDbType DbType{get;set;}
//public SqlDbType? DbType{get;set;} 这个情况下,就出"不是有效的属性实参,因为它不是有效的命名属性形参类型"
}