我正在使用从属性类继承的自定义属性.我这样使用它:
[MyCustomAttribute("CONTROL")]
[MyCustomAttribute("ALT")]
[MyCustomAttribute("SHIFT")]
[MyCustomAttribute("D")]
public void setColor()
{
}
Run Code Online (Sandbox Code Playgroud)
但是显示了"Duplicate'MyCustomAttribute'属性"错误.
如何创建重复的允许属性?
我从这个链接中看到了这个答案.将参数添加到自定义属性中,如何在自定义属性上添加参数
class MyCustomAttribute : Attribute {
public int[] Values { get; set; }
public MyCustomAttribute(params int[] values) {
this.Values = values;
}
}
[MyCustomAttribute(3, 4, 5)]
class MyClass { }
Run Code Online (Sandbox Code Playgroud)
现在我想知道它是不是可以像这样写?
class MyCustomAttribute : Attribute {
private int[] _values;
public MyCustomAttribute(params int[] values) {
_values = values;
}
}
[MyCustomAttribute(3, 4, 5)]
class MyClass { }
Run Code Online (Sandbox Code Playgroud)
我将属性值更改为变量_values.我也将它设为私有,当我尝试它时它工作正常.
有人可以告诉我为什么接受的答案是有效的吗?
我如何解析List自定义动作过滤器(如输入参数)?
public class CustomFilter : ActionFilterAttribute
{
public List<MyEnumType> InputParameter { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
}
[CustomFilter(InputParameter = new List<MyEnumType>() { MyEnumType.Type } )]
public SomeActionInController()
{
}
Run Code Online (Sandbox Code Playgroud)
我得错了错误
'InputParameter' is not a valid named attribute argument because it is not a valid attribute parameter type
Run Code Online (Sandbox Code Playgroud) C#中的属性可以与集合初始值设定项一起使用吗?
例如,我想做类似以下的事情:
[DictionaryAttribute(){{"Key", "Value"}, {"Key", "Value"}}]
public class Foo { ... }
Run Code Online (Sandbox Code Playgroud)
我知道属性可以有命名参数,因为这看起来与对象初始化器非常相似,我想知道集合初始化器是否也可用.
c# attributes custom-attributes object-initializers collection-initializer
我有一个自定义的AuthorizeAttribute类,用于处理我的MVC4应用程序中的粒度授权.
这是班级:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class isAuthorized : AuthorizeAttribute
{
public oRoles enRole;
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var authorized = base.AuthorizeCore(httpContext);
string test = enRole.ToString();
if (!authorized)
{
// The user is not authenticated
return false;
}
var user = httpContext.User;
bool bFlag = AuthCheck.CheckUser(httpContext, enRole);
if (bFlag) // I know this is a lot of code; it's for debugging purposes
return true;
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经声明了以下枚举以允许代码帮助:
public enum oRoles
{
StudentSelfPassword = 1,
StaffSelfPassword …Run Code Online (Sandbox Code Playgroud) 我想在我的mvc视图模型中使用数据注释Range属性。问题在于此范围属性应为动态值。
我的视图模型还具有ValueOne和ValueTwo属性。基于此值,我要设置范围属性。像
[Range(1, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
Run Code Online (Sandbox Code Playgroud)
其中1和1000应替换为ValueOne和ValueTwo属性值。
所以我尝试使用自定义ValidateCustomAttribute
public class ValidateCustomAttribute: ValidationAttribute
{
private readonly double _MinValue = 0;
private readonly double _MaxValue = 100;
public override bool IsValid(object value)
{
double val = (double)value;
return val >= _MinValue && val <= _MaxValue;
}
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessage, _MinValue, _MaxValue);
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何替换
private readonly double _MinValue = 0;
private readonly double _MaxValue = 100;
Run Code Online (Sandbox Code Playgroud)
具有动态值(我的视图模型中的ValueOne和ValueTwo)。
我想将一些枚举列表传递给我的Attribute属性.但是你可以将List传递给Attribute的属性.所以我尝试将其转换为字符串表示并尝试执行以下操作:
[MyAtt(Someproperty =
Enums.SecurityRight.A.ToString() + "&" + (Enums.SecurityRight.B.ToString() ))]
Run Code Online (Sandbox Code Playgroud)
但是,这会给出错误:"属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式"
我知道你只能传递常数.但是我怎么逃避这个呢?任何招数?
谢谢.
考虑这样的 ExcelDNA 函数定义:
[ExcelFunction(Name = "Fnc1", Description = "Fnc1")]
public static object Fnc1(
[ExcelArgument(Name = "Arg1", Description = "Arg1", AllowReference = true)]
object rng)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
=Fnc1(A1)或这样的连续单元格范围调用时,它工作正常=Fnc1(A1:A3)。=Fnc1(A1,A5,A10). #VALUE!返回错误。有没有办法如何调用具有不连续范围的未知单元格数量的ExcelDNA 函数?
我已经尝试像这样声明参数,params object[] rng但也没有运气。