标签: custom-attributes

ASP.NET MVC如何应用基于角色或基于身份验证的View呈现?

我想根据身份验证状态或角色显示/隐藏视图的某些部分.对于我的控制器操作,我已经扩展了ActionFilterAttribute,因此我可以归因于某些操作.

<RequiresRole(Role:="Admin")> _
Function Action() as ActionResult
    Return View()
End Function
Run Code Online (Sandbox Code Playgroud)

是否有一个类似的(归属)的方式,我可以在浏览使用?(所以不喜欢这样:如何根据用户所处的角色创建具有不同显示的视图?)

asp.net-mvc view custom-attributes

6
推荐指数
1
解决办法
4750
查看次数

所有主流浏览器都支持HTML 5吗?

我在这里查看html 5的自定义属性功能 http://ejohn.org/blog/html-5-data-attributes/

当我使用jquery/javascript时,这看起来很完美.

我的问题,所有主流浏览器都支持HTML 5吗?

<li class="user" data-name="John Resig" data-city="Boston"
     data-lang="js" data-food="Bacon">
  <b>John says:</b> <span>Hello, how are you?</span>
</li>
Run Code Online (Sandbox Code Playgroud)

html javascript jquery html5 custom-attributes

6
推荐指数
1
解决办法
9297
查看次数

C#Generic - 任何声明T的方法都有属性?

[MyCustomClassAttribute]
public class Foo
{
}


public class Bar<T>
    where T : ??
{
    public T FooInstance;
}
Run Code Online (Sandbox Code Playgroud)

我很确定这是不可能的,但我想我会检查一下.任何方式使T必须有一个[MyCustomClassAttribute]吗?

c# generics custom-attributes

6
推荐指数
1
解决办法
1248
查看次数

jQuery和数据属性来处理所有ajax调用?

我正在考虑通过在属性链接上启用ajax来减少javascript代码量的方法.例:

<a href="/Default/Link.html" data-endpoint="/Ajax/Link.html" rel="targetId" async="true">Click me!</a>
Run Code Online (Sandbox Code Playgroud)

async="true"将禁用link(href)的默认行为,并使用该data-endpoint值执行ajax调用并将其插入到中定义的元素id rel.

我不是JS专家,所以我很欣赏使用这种方法的任何想法或陷阱.诸如cache:true等选项也很酷,但也可以传入,但不是真的需要,因为我想这样做以获得包含或多或少的实时数据的部分视图(不需要缓存).

(这是从我在facebook上如何最小化他们的代码的一个话题中得到启发,但与他们如何优化所有内容到每个位'n字节相比可能非常简化)

ajax jquery custom-attributes

6
推荐指数
1
解决办法
9935
查看次数

自定义属性类是否继承"Inherited"AttributeUsage标志?

请考虑以下情形:

  • 甲基属性类BaseAttribute具有AttributeUsageAttribute指定它不是可继承(Inherited = False).
  • 派生属性类DerivedAttribute继承自该基本属性类.
  • 基本域类Base已应用派生属性.
  • Derived要求从基域类继承的域类具有其自定义属性,包括继承的属性(inherit: true).

这是相应的代码:

using System;
using System.Linq;

namespace ConsoleApplication26
{
  class Program
  {
    static void Main ()
    {
      var attributes = typeof (Derived).GetCustomAttributes (true);
      foreach (var attribute in attributes)
      {
        Console.WriteLine (
            "{0}: Inherited = {1}",
            attribute.GetType().Name,
            attribute.GetType().GetCustomAttributes (typeof (AttributeUsageAttribute), true).Cast<AttributeUsageAttribute>().Single().Inherited);
      }
    }
  }

  [AttributeUsage (AttributeTargets.All, Inherited = false)]
  public class BaseAttribute : Attribute
  {
  }

  public class DerivedAttribute : BaseAttribute …
Run Code Online (Sandbox Code Playgroud)

c# reflection custom-attributes

6
推荐指数
1
解决办法
1690
查看次数

C#属性类的继承

属性类的继承如何在C#中起作用?为了澄清,我说的是属性类本身的继承(即基类System.Attributes),而不是任何碰巧将它们作为属性的类.

出于某种原因,我似乎无法找到任何东西(我的搜索总是出现其他含义).

例如,如果a有一个AttributeA扩展的类System.Attribute:

  1. 我必须单独标记的子AttributeA[AttributeUsage()].(既然System.AttributeUsage继承了true,我认为不会.)

  2. AllowMultiple=falseAttributeUsageAttributeA阻止我有多个子类AttributeA的属性?

编辑:

程序员只能读代码.

[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
class AttributeA
{ }

class AttributeB : AttributeA
{ }

class AttributeC : AttributeA
{ }

[AttributeB]
[AttributeC]
class Foo
{ }
Run Code Online (Sandbox Code Playgroud)

这有用吗?

.net c# inheritance custom-attributes

6
推荐指数
1
解决办法
1725
查看次数

数据注释 - 使用属性扩展并在资源文件中存储正则表达式

我目前正在使用MVC4数据注释来处理验证.我正在开发一个非常国际化的网站,因此我将所有文本保存在资源文件中.

我还想在资源文件中保留正则表达式以进行验证,这样我就可以使用相同的代码来检查,例如,Post Codes(UK)Zip Codes(US),只需使用不同的RegEx(以及不同名称的资源等) ).

我有以下属性已经从资源文件中提取错误消息.我怎样才能从资源文件中获取正则表达式?

[RegularExpression(@"^[\w]{1,2}[0-9]{1,2}[\w]?\s?[0-9]{1,2}[\w]{1,2}$", ErrorMessageResourceType = typeof(Resources.ValidationMessages), ErrorMessageResourceName = "validPostcode")]
Run Code Online (Sandbox Code Playgroud)

编辑(再次)

我现在在哪里

按照下面的答案和一些额外的搜索,我有以下内容:

Global.asax.cs我添加了以下行,以确保调用客户端验证

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalisedAttribute), typeof(RegularExpressionAttributeAdapter));
Run Code Online (Sandbox Code Playgroud)

在我的模型中,我调用了属性扩展

[Localised(typeof(Resources.FormValidation), "postcodeRegEx", "postcodeMsg")]
Run Code Online (Sandbox Code Playgroud)

最后,本地化正则表达式验证的属性扩展

public class LocalisedAttribute : RegularExpressionAttribute
{
    public LocalisedAttribute(Type resource, string regularExpression, string errorMessage) 
        : base(GetRegex(regularExpression))
    {
        ErrorMessageResourceType = resource;
        ErrorMessageResourceName = errorMessage;
    }

    private static string GetRegex(string value) 
    {
        return Resources.FormValidation.ResourceManager.GetString(value);


    }
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,但只是我第一次在启动应用程序时使用它.

我将打开另一个问题来解决这个问题 - 它与原始请求没有直接关系,似乎与大多数人的实现没有关系,似乎并不特定于数据注释.

asp.net asp.net-mvc custom-attributes data-annotations asp.net-mvc-4

6
推荐指数
1
解决办法
1819
查看次数

在属性中键入约束

我想用自定义属性编写枚举,例如:

public enum SomeEnum: long
{
    [SomeAttribute<MyClass1>]
    Sms = 1,
    [SomeAttribute<MyClass2>]
    Email = 2
}
Run Code Online (Sandbox Code Playgroud)

但属性不支持泛型.嗯,最类似的解决方案是:

public enum SomeEnum: long
{
    [SomeAttribute(typeof(MyClass1))]
    Sms = 1,
    [SomeAttribute(typeof(MyClass2))]
    Email = 2
}
Run Code Online (Sandbox Code Playgroud)

这里有问题:我想Class1继承ICustomInterface,所以使用泛型我可以编写约束:

[AttributeUsage(AttributeTargets.All)]
class SomeAttribute<T> : Attribute where T: ICustomInterface
{
}
Run Code Online (Sandbox Code Playgroud)

但属性不支持泛型.

所以最后的问题是:如何检查编译时间(如T约束)该类型是否正在实现某些接口?

.net c# generics reflection custom-attributes

6
推荐指数
1
解决办法
3063
查看次数

从Attribute转到CustomAttributeData或向后

题.有没有办法CustomAttributeData根据自定义属性的给定实例获取实例,比如说MyAttribute?或相反亦然?

我为什么需要这个?实例MyAttribute包含我感兴趣的属性,同时实例CustomAttributeData包含实际的构造函数的参数我很感兴趣,所以,现在我实现了双重的工作:第一,得到的实例MyAttribute调用

Attribute.GetCustomAttribute(property, typeof(MyAttribute)) as MyAttribute
Run Code Online (Sandbox Code Playgroud)

,第二,CustomAttributeData通过调用获取实例

CustomAttributeData.GetCustomAttributes(property)
Run Code Online (Sandbox Code Playgroud)

并走过这个系列.

PS我已经看过这个问题,但没有在那里找到理想的解决方案.

c# reflection custom-attributes system.reflection

6
推荐指数
1
解决办法
2427
查看次数

C#.NET CORE如何获取自定义属性的值?

我有一个自定义属性类定义如下.

[AttributeUsage(AttributeTargets.Property, Inherited = false)]
internal class EncryptedAttribute : System.Attribute
{
    private bool _encrypted;
    public EncryptedAttribute(bool encrypted)
    {
        _encrypted = encrypted;
    }

    public virtual bool Encrypted
    {
        get
        {
            return _encrypted;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我将以上属性应用于另一个类,如下所示.

public class KeyVaultConfiguration
{
    [Encrypted(true)]
    public string AuthClientId { get; set; } = "";

    public string AuthClientCertThumbprint { get; set; } = "";
}
Run Code Online (Sandbox Code Playgroud)

如何在属性AuthClientId上获取Encrypted = True的值?

var config = new KeyVaultConfiguration();

// var authClientIdIsEncrypted = ??
Run Code Online (Sandbox Code Playgroud)

在.NET Framework中,这很容易.在.NET CORE中,我认为这是可能的,但我没有看到任何文档.我相信你需要使用System.Reflection但究竟如何?

c# custom-attributes .net-core

6
推荐指数
1
解决办法
8561
查看次数