标签: custom-attributes

HTML5自定义属性 - 我为什么要使用它们?

我似乎无法理解为什么我应该对HTML5允许自定义属性感到满意?我为什么要用它们?

html5 custom-attributes

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

模型绑定时在视图模型属性上查找自定义属性

我发现了很多关于为验证目的实现自定义模型绑定器的信息,但我还没有看到我正在尝试做什么.

我希望能够根据视图模型中属性的属性来操作模型绑定器要设置的值.例如:

public class FooViewModel : ViewModel
{
    [AddBar]
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

AddBar就是

public class AddBarAttribute : System.Attribute
{
}
Run Code Online (Sandbox Code Playgroud)

我无法在自定义模型绑定器的BindModel方法中找到一种在查看模型属性上查找属性的简洁方法.这有效,但感觉应该有一个更简单的解决方案:

public class FooBarModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = base.BindModel(controllerContext, bindingContext);

        var hasBarAttribute = false;

        if(bindingContext.ModelMetadata.ContainerType != null)
        {
            var property = bindingContext.ModelMetadata.ContainerType.GetProperties()
                .Where(x => x.Name == bindingContext.ModelMetadata.PropertyName).FirstOrDefault();
            hasBarAttribute = property != null && property.GetCustomAttributes(true).Where(x => x.GetType() == typeof(AddBarAttribute)).Count() > 0;
        }

        if(value.GetType() == typeof(String) && …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc custom-attributes model-binding

15
推荐指数
1
解决办法
7287
查看次数

是否有类似于C#属性的TypeScript

在C#中,有一种语法可用于定义属性的属性.

[Required]
string personName
Run Code Online (Sandbox Code Playgroud)

它描述了personName是必需的.我们可以通过反射在任何给定时间内获取属性的属性.

我想知道TypeScript是否有这样的功能?

c# custom-attributes typescript

15
推荐指数
2
解决办法
1511
查看次数

J2EE:自定义标记属性的默认值

因此,根据Sun的J2EE文档(http://docs.sun.com/app/docs/doc/819-3669/bnani?l=en&a=view),"如果不需要标记属性,标记处理程序应该提供默认值."

我的问题是如何根据文档的描述定义默认值.这是代码:

<%@ attribute name="visible" required="false" type="java.lang.Boolean" %>
<c:if test="${visible}">
     My Tag Contents Here
</c:if>
Run Code Online (Sandbox Code Playgroud)

显然,这个标签不会编译,因为它缺少tag指令和核心库导入.我的观点是,我希望"visible"属性默认为TRUE."标签属性不是必需的",因此"标签处理程序应提供默认值".我想提供一个默认值,所以我错过了什么?

任何帮助是极大的赞赏.

java jsp-tags custom-attributes java-ee

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

获取ASP.NET MVC中当前操作/控制器的自定义属性列表

查看为ASP.NET MVC2编写的http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc中的示例代码,我注意到他们可以检查自定义属性是否为通过访问filterContext.ActionDescriptorfilterContext.ActionDescriptor.ControllerDescriptor分别应用于当前操作或控制器:

public class ExitHttpsIfNotRequiredAttribute : FilterAttribute, IAuthorizationFilter {
    public void OnAuthorization(AuthorizationContext filterContext) {
        // snip

        // abort if a [RequireHttps] attribute is applied to controller or action
        if(filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return;
        if(filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return;

        // snip
    }
}
Run Code Online (Sandbox Code Playgroud)

检查自定义属性的操作和控制器的ASP.NET MVC 1方法是什么?在ASP.NET MVC 1中filterContext.ActionDescriptor,我无法分辨.

asp.net-mvc custom-attributes authorize-attribute

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

是否可以使用JS变量通过jQuery attr()函数设置自定义属性

我正在使用jquery 1.5和html4标准.
我正在尝试设置我通过javascript变量获取的自定义属性,但它没有设置up.code示例:

var attname="list1"; // this is changed on every call of the function where it is defined.    
var attvalue="b,c,d"; // this also changed.  
jQuery('#div1').attr({attname:attvalue});
Run Code Online (Sandbox Code Playgroud)

但它将attname视为字符串本身而非变量.还有其他选项,比如使用data(),prop()但是HTML5和jquery 1.6支持它们,这对我来说是不可能的.另外一个问题是无法在服务器端设置数据以便同步和使用客户端通过jquery data().因为它们在语法上是差异的.如果有其他方式请建议谢谢.

jquery custom-attributes

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

WCF服务属性用于记录方法调用和异常

我需要在WCF服务中记录每个方法调用,并抛出任何异常.这导致了许多冗余代码,因为每个方法都需要包含类似于此的样板:

[OperationContract]
public ResultBase<int> Add(int x, int y)
{
    var parameters = new object[] { x, y }
    MyInfrastructure.LogStart("Add", parameters);
    try
    {
        // actual method body goes here
    }
    catch (Exception ex)
    {
        MyInfrastructure.LogError("Add", parameters, ex);
        return new ResultBase<int>("Oops, the request failed", ex);
    }
    MyInfrastructure.LogEnd("Add", parameters);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法可以将所有这些逻辑封装到一个属性中MyServiceLoggingBehaviorAttribute,我可以将其应用于服务类(或方法),如下所示:

[ServiceContract]
[MyServiceLoggingBehavior]
public class MyService
{
}
Run Code Online (Sandbox Code Playgroud)

注意#1

我意识到这可以使用面向方面的编程来完成,但在C#中,唯一的方法是修改字节码,这需要使用像PostSharp这样的第三方产品.我想避免使用商业图书馆.

笔记2

请注意,Silverlight应用程序是该服务的主要使用者.

注意#3

在某些情况下,WCF跟踪日志记录是一个不错的选择,但它在这里不起作用,因为如上所述,我需要检查,并且在异常更改的情况下,返回值.

c# wcf logging aop custom-attributes

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

在使用JSON.NET序列化对象时,如何添加自定义根节点?

我已经为我的一些对象添加了一个自定义属性,如下所示:

[JsonCustomRoot("status")]
public class StatusDTO 
{
    public int StatusId { get; set; }
    public string Name { get; set; }
    public DateTime Created { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

属性非常简单:

public class JsonCustomRoot :Attribute
{
    public string rootName { get; set; }

    public JsonCustomRoot(string rootName)
    {
        this.rootName = rootName;
    }
}
Run Code Online (Sandbox Code Playgroud)

序列化对象实例时JSON.NET的默认输出是:

{"StatusId":70,"Name":"Closed","Created":"2012-12-12T11:50:56.6207193Z"}
Run Code Online (Sandbox Code Playgroud)

现在的问题是:如何使用自定义属性的值向JSON添加根节点,如下所示:

{status:{"StatusId":70,"Name":"Closed","Created":"2012-12-12T11:50:56.6207193Z"}}
Run Code Online (Sandbox Code Playgroud)

我发现有几篇提到IContractResolver接口的文章,但我无法理解如何做到这一点.我的尝试包括这段未完成的代码:

protected override JsonObjectContract CreateObjectContract(Type objectType)
{
    JsonObjectContract contract = base.CreateObjectContract(objectType);

    var info = objectType.GetCustomAttributes()
                   .SingleOrDefault(t => (Type)t.TypeId==typeof(JsonCustomRoot));
    if (info != null) …
Run Code Online (Sandbox Code Playgroud)

c# serialization json custom-attributes json.net

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

MVC3 - 放置自定义属性类的位置

我正在深入研究自定义验证属性,并且很想知道其他人如何构建项目.您通常在哪里存储自定义属性?

我的第一个想法是简单地创建一个新文件夹并完成它.

有什么建议?

custom-attributes asp.net-mvc-3

13
推荐指数
2
解决办法
7265
查看次数

如何将方法参数插入自定义属性

我有一个名为AuthoriseAttribute的自定义属性,其构造函数如下所示:

public AuthoriseAttribute(int userId)
{
  .. blah
}
Run Code Online (Sandbox Code Playgroud)

这与一个这样的方法一起使用GetUserDetails():

[Authorise(????????)]
public UserDetailsDto GetUserDetails(int userId)
{
  .. blah
}
Run Code Online (Sandbox Code Playgroud)

在运行时,Authorize属性的存在会导致执行某些授权代码,这需要用户的ID.显然,这可以从GetUserDetails()方法的参数中提取,但这意味着授权代码取决于方法的参数被赋予特定名称.

我希望能够将userId参数的实际值传递给属性,以便授权代码使用传递给属性的值(即不是方法参数),其名称已知.

像这样的东西(不起作用):

[Authorise(userId)]
public UserDetailsDto GetUserDetails(int userId)
{
  .. blah
}
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

c# custom-attributes

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