标签: custom-attributes

类成员的自定义属性

我正在使用自定义属性来定义类的成员如何映射到属性以作为表单发布(支付网关)发布.我有自定义属性工作正常,并能够通过"名称"获取属性,但希望成员本身获取属性.

例如:

getFieldName("name");
Run Code Online (Sandbox Code Playgroud)

VS

getFieldName(obj.Name);
Run Code Online (Sandbox Code Playgroud)

计划是编写一个方法,将带有成员的类序列化为一个postable字符串.

这是我此时的测试代码,其中ret是一个字符串,PropertyMapping是自定义属性:

foreach (MemberInfo i in (typeof(CustomClass)).GetMember("Name"))
{
    foreach (object at in i.GetCustomAttributes(true))
    {
        PropertyMapping map = at as PropertyMapping;
        if (map != null)
        {
            ret += map.FieldName;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

c# reflection attributes custom-attributes

8
推荐指数
1
解决办法
3210
查看次数

将属性限制为类或属性是否可行?

我有两个自定义属性定义如下:

internal class SchemaAttribute : Attribute {
    internal SchemaAttribute(string schema) {
        Schema = schema;
    }

    internal string Schema { get; private set; }
}

internal class AttributeAttribute : Attribute {
    internal AttributeAttribute(string attribute) {
        Attribute = attribute;
    }

    internal string Attribute { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

我想将SchemaAttribute限制为类,将AttributeAttribute限制为属性.

这可行吗?

c# vb.net attributes custom-attributes .net-2.0

8
推荐指数
1
解决办法
1488
查看次数

使用C#自定义属性进行异常和审计跟踪日志记录

是否可以创建一个自定义功能来捕获由自定义属性设置的方法中的异常?

我打算做这样的事情:

[Logging(FeatureEnum.SomeFeature, IntentEnum.SomeIntent, "some comment")]
public void SomeMethodThatDoesATask()
{
    try
    {
      var doSomeAction = new LazyProcess();
      doSomeAction.WhoDunnit();
    }
    catch(Exception ex)
    {
       StaticMethodThatDoesLogging.CatchError(ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是:如何捕获放置此属性的方法名称以及抛出的事件?它可以捕获异常,也可以自动记录调用此方法.

c# logging custom-attributes

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

Java覆盖抽象泛型方法

我有以下代码

public abstract class Event {
    public void fire(Object... args) {
        // tell the event handler that if there are free resources it should call 
        // doEventStuff(args)
    }

    // this is not correct, but I basically want to be able to define a generic 
    // return type and be able to pass generic arguments. (T... args) would also 
    // be ok
    public abstract <T, V> V doEventStuff(T args);
}

public class A extends Event {
   // This is what I …
Run Code Online (Sandbox Code Playgroud)

java generics abstract-class custom-attributes

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

MVC ActionFilter类似于WCF的属性

有没有办法可以为WCF创建自定义方法属性,允许我使用预过滤器轻松装饰服务方法,就像MVC使用动作过滤器一样.我打算将它们用于身份验证和授权.这适用于RESTful服务,其请求带有身份验证cookie.

我更感兴趣的是如何创建属性而不是身份验证方面.HTTP Toolkit是否提供任何功能?

谢谢

wcf custom-attributes action-filter

8
推荐指数
1
解决办法
2345
查看次数

如何从自定义属性中引用android属性layout_width?

我想将我的应用程序在xlarge设备上的使用和其他设备上的使用分开,将layout_width参数限制为xdge的720dp.为此我创建了values/attrs.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <attr name="layoutWidth" format="reference|dimension" />
</resources>
Run Code Online (Sandbox Code Playgroud)

使用自定义参数layoutWidth将其设置为

android:layout_width="?layoutWidth"
Run Code Online (Sandbox Code Playgroud)

Furtner,我需要在values-xlargevalues目录中为xlarge和普通设备指定两个themes.xml文件 :

值-XLARGE /的themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
        <item name="layoutWidth">720dp</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

值/的themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
        <item name="layoutWidth">What should I write here!?</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

那么,如何在这个地方对Android"fill_parent"参数进行参考呢?好像是@android:layout_width/fill_parent,但是我编译错误:

 No resource found that matches the given name (at 'layoutWidth' with value '@android:layout_width/fill_parent').
Run Code Online (Sandbox Code Playgroud)

layout android attributes custom-attributes

8
推荐指数
1
解决办法
9654
查看次数

授权属性中的UrlHelper和ViewContext

我有一个我无法解决的场景:

我正在为mvc创建自己的自定义授权属性.我想添加的主要功能是能够更改用户重定向的位置(如果用户不在某个角色中).我不介意系统发送他们回到登录页面,如果他们没有通过身份验证,但我想选择在那里给他们,如果他们进行身份验证,但不允许访问该操作方法.

这是我想做的事情:

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
        public string Action;
        public string Controller;

        protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
        {
            // if User is authenticated but not in the correct role
            string url = Url.Action(this.Action, this.Controller);                
            httpContext.Response.Redirect(url);
        }
    }
Run Code Online (Sandbox Code Playgroud)

作为一个额外的奖励,我想在进行重定向之前访问ViewContext和TempData.

有关如何在属性中实例化UrlHelper和ViewContext的任何想法?

asp.net asp.net-mvc asp.net-membership custom-attributes httpcontext

7
推荐指数
1
解决办法
1880
查看次数

可以在ASP.NET MVC中的ValidationAttribute中访问HttpContext吗?

可以在ASP.NET MVC 3中HttpContext访问ValidationAttribute吗?

我需要在路由数据中测试一些匹配项,以便在我的验证器上返回true.

谢谢

.net validation asp.net-mvc custom-attributes

7
推荐指数
1
解决办法
2678
查看次数

修剪除具有NoTrim属性的输入字段以外的每个输入字段

我正在开发一个我没有创建的ASP.NET MVC 2应用程序.在模型绑定期间修剪应用程序中的所有输入字段.但是,我想要一个NoTrim属性来阻止修剪某些字段.

例如,我有以下状态下拉字段:

<select name="State">
    <option value="">Select one...</option>
    <option value="  ">International</option>
    <option value="AA">Armed Forces Central/SA</option>
    <option value="AE">Armed Forces Europe</option>
    <option value="AK">Alaska</option>
    <option value="AL">Alabama</option>
    ...
Run Code Online (Sandbox Code Playgroud)

问题是当用户选择"国际"时,我得到验证错误,因为两个空格被修剪,State是必填字段.

这是我希望能够做到的:

    [Required( ErrorMessage = "State is required" )]
    [NoTrim]
    public string State { get; set; }
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所获得的属性:

[AttributeUsage( AttributeTargets.Property, AllowMultiple = false )]
public sealed class NoTrimAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)

在Application_Start中设置了一个自定义模型绑定器:

protected void Application_Start()
{
    ModelBinders.Binders.DefaultBinder = new MyModelBinder();
    ...
Run Code Online (Sandbox Code Playgroud)

以下是修剪模型活页夹的一部分:

protected override void SetProperty( ControllerContext controllerContext,
                                     ModelBindingContext bindingContext,
                                     PropertyDescriptor propertyDescriptor,
                                     object value …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc customvalidator custom-attributes model-binding data-annotations

7
推荐指数
1
解决办法
2527
查看次数

c# - 从属性构造函数中抛出异常

我找到了关于这个主题的这篇文章并尝试了以下内容:

public class FailerAttr : Attribute {
    public FailerAttr(string s) {
        throw new Exception("I should definitely fail!");
    }
}
Run Code Online (Sandbox Code Playgroud)

在单元测试项目中,我有以下内容:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class Test {
    [TestMethod]
    public void GoFail() {
        // Make sure attribute will get initialized
        new Failer();
    }

    private class Failer {
        [FailerAttr("")]
        public int Prop { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,它会成功.所以,问题是:

  1. 为什么不失败?
  2. 从属性中抛出异常真是个坏主意吗?因为我认为我需要.

一些环境信息(以防万一相关):

  • 单元测试通过ReSharper的单元测试运行器(R#v8.2.0.2160)运行
  • Visual studio v11.0.61030.0

c# exception custom-attributes

7
推荐指数
2
解决办法
3699
查看次数