标签: custom-attributes

是否可以将委托作为属性参数?

是否可以将委托作为属性的参数?

像这样:

public delegate IPropertySet ConnectionPropertiesDelegate();

public static class TestDelegate
{
    public static IPropertySet GetConnection()
    {
        return new PropertySetClass();
    }
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface,AllowMultiple=false,Inherited=true)]
public class WorkspaceAttribute : Attribute
{
    public ConnectionPropertiesDelegate ConnectionDelegate { get; set; }

    public WorkspaceAttribute(ConnectionPropertiesDelegate connectionDelegate)
    {
        ConnectionDelegate = connectionDelegate;
    }
}

[Workspace(TestDelegate.GetConnection)]
public class Test
{
}
Run Code Online (Sandbox Code Playgroud)

如果不可能,那么明智的选择是什么?

c# delegates custom-attributes

43
推荐指数
2
解决办法
2万
查看次数

我应该为枚举成员使用什么AttributeTarget?

我想用我IsGPUBasedAttribute的enum成员这样:

public enum EffectType
{
    [IsGPUBased(true)]
    PixelShader,

    [IsGPUBased(false)]
    Blur
}
Run Code Online (Sandbox Code Playgroud)

但编译器不允许我使用:

[AttributeUsage (AttributeTargets.Enum, AllowMultiple = false)]
Run Code Online (Sandbox Code Playgroud)

AttributeTarget限制enum成员使用的正确价值是什么?

.net c# enums custom-attributes

41
推荐指数
2
解决办法
1万
查看次数

如何获取枚举的自定义属性值?

我有一个枚举,每个成员都有一个自定义属性应用于它.如何检索存储在每个属性中的值?

现在我这样做:

var attributes = typeof ( EffectType ).GetCustomAttributes ( false );
foreach ( object attribute in attributes )
{
    GPUShaderAttribute attr = ( GPUShaderAttribute ) attribute;
    if ( attr != null )
        return attr.GPUShader;
}
return 0;
Run Code Online (Sandbox Code Playgroud)

另一个问题是,如果找不到,我应该返回什么?0是可以转换为任何枚举,对吧?这就是我回归的原因.

忘记提及,上面的代码为每个枚举成员返回0.

.net c# reflection custom-attributes

40
推荐指数
3
解决办法
4万
查看次数

属性构造函数中的Lambda表达式

我创建了一个Attribute名为的类RelatedPropertyAttribute:

[AttributeUsage(AttributeTargets.Property)]
public class RelatedPropertyAttribute: Attribute
{
    public string RelatedProperty { get; private set; }

    public RelatedPropertyAttribute(string relatedProperty)
    {
        RelatedProperty = relatedProperty;
    }
}
Run Code Online (Sandbox Code Playgroud)

我用它来表示类中的相关属性.我将如何使用它的示例:

public class MyClass
{
    public int EmployeeID { get; set; }

    [RelatedProperty("EmployeeID")]
    public int EmployeeNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想使用lambda表达式,以便我可以将强类型传递给我的属性的构造函数,而不是"魔术字符串".这样我可以利用编译器类型检查.例如:

public class MyClass
{
    public int EmployeeID { get; set; }

    [RelatedProperty(x => x.EmployeeID)]
    public int EmployeeNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我以为我可以使用以下内容,但编译器不允许这样做:

public RelatedPropertyAttribute<TProperty>(Expression<Func<MyClass, TProperty>> propertyExpression)
{ ... } …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection attributes custom-attributes

40
推荐指数
5
解决办法
2万
查看次数

向客户实体添加属性

我目前的目标是添加一个新的客户属性(带有int类型),该属性应该显示为具有预定义选项的select(从具有可在后端编辑的条目的模型加载,这已完成).我正在努力正确使用$installer->addAttribute()方法,特别是指定正确的源选项.其他问题是新属性未保存到eav_entity_attribute表

我在Magento CE 1.5.1.0上

php custom-attributes magento

37
推荐指数
2
解决办法
3万
查看次数

从枚举属性获取枚举

我有

public enum Als 
{
    [StringValue("Beantwoord")] Beantwoord = 0,
    [StringValue("Niet beantwoord")] NietBeantwoord = 1,
    [StringValue("Geselecteerd")] Geselecteerd = 2,
    [StringValue("Niet geselecteerd")] NietGeselecteerd = 3,
}
Run Code Online (Sandbox Code Playgroud)

public class StringValueAttribute : Attribute
{
    private string _value;

    public StringValueAttribute(string value)
    {
        _value = value;
    }

    public string Value
    {
        get { return _value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将我从组合框中选择的项的值放入int:

int i = (int)(Als)Enum.Parse(typeof(Als), (string)cboAls.SelectedValue); //<- WRONG
Run Code Online (Sandbox Code Playgroud)

这是可能的,如果是的话,怎么样?(StringValue匹配从组合框中选择的值).

.net c# enums attributes custom-attributes

33
推荐指数
3
解决办法
2万
查看次数

Android片段中的自定义属性

我想在Android片段中使用XML定义自定义属性(不使用捆绑其他参数),就像declare-styleable在自定义控件中一样.但是没有带有AttrSet参数的构造函数,所以它可能吗?我可以覆盖public void onInflate(android.app.Activity activity, android.util.AttributeSet attrs, android.os.Bundle savedInstanceState)以获得属性支持吗?

android custom-attributes fragment android-fragments

33
推荐指数
2
解决办法
1万
查看次数

属性参数必须是常量表达式,...-创建类型为array的属性

这是我的自定义属性和我正在使用它的类:

[MethodAttribute(new []{new MethodAttributeMembers(), new MethodAttributeMembers()})]
public class JN_Country
{

}

public class MethodAttribute : Attribute
{
    public MethodAttributeMembers[] MethodAttributeMembers { get; set; }

    public MethodAttribute(MethodAttributeMembers[] methodAttributeMemberses)
    {
        MethodAttributeMembers = methodAttributeMemberses;
    }
}

public class MethodAttributeMembers
{
    public string MethodName { get; set; }
    public string Method { get; set; }
    public string MethodTitle { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

语法错误,显示在上面的第一行:

属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

为什么会出现此错误?

c# custom-attributes

33
推荐指数
2
解决办法
6万
查看次数

Android中的自定义属性

我正在尝试为所有可编辑元素创建一个名为Tag的自定义属性.我将以下内容添加到attrs.xml中

<declare-styleable name="Spinner">
    <attr name="tag" format="string" />
</declare-styleable>

<declare-styleable name="EditText">
    <attr name="tag" format="string" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

我收到一条错误,说"EditText已经定义了属性标记".是否无法在不同的元素上创建同名的自定义属性?

android custom-attributes

30
推荐指数
2
解决办法
1万
查看次数

实现FilterAttribute,IActionFilter和从asp.net mvc 3中的ActionFilterAttribute继承有什么区别?

我看到在一种情况下我们可以覆盖OnActionExecutingOnActionExecuted继承ActionFilterAttribute类的方法,如下所示:

public class MyFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    { // bla bla }
}
Run Code Online (Sandbox Code Playgroud)

在其他情况下,我们也可以实现IActionFilterFilterAttribute喜欢这样:

public class MySecondFilterAttribute : FilterAttribute, IActionFilter 
{ 
    public void OnActionExecuted(ActionExecutingContext filterContext) {}
}
Run Code Online (Sandbox Code Playgroud)

那么,这两种方法之间是否有任何差异,可能是任何特殊情况下,最好使用其中一种方法而不是另一种方法?

提前致谢.

asp.net attributes filter custom-attributes asp.net-mvc-3

30
推荐指数
1
解决办法
1万
查看次数