标签: custom-attributes

使用Jquery基于自定义属性值从ul中删除li

我有多个带有自定义属性的ul:

<ul data-id="1">
  <li data-li-id="1">This</li>
  <li data-li-id="2">That</li>
  <li data-li-id="3">Here</li>
</ul>


<ul data-id="2">
  <li data-li-id="1">This</li>
  <li data-li-id="2">That</li>
  <li data-li-id="3">Here</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我知道列表的data-id和li的data-li-id,我想从列表中删除li.到目前为止,我有:

 $("ul[data-id=" + data.listId + "] > li").attr('[data-li-id="' + data.listItemId + '"]').remove();
Run Code Online (Sandbox Code Playgroud)

但无法让它发挥作用.任何帮助,将不胜感激.

jquery jquery-ui custom-attributes

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

具有多个XmlArrayItemAttribute实例的GetCustomAttribute

我有一个List<TransformationItem>.TransformationItem只是多个类的基类,例如ExtractTextTransformInsertTextTransform

为了使用内置的XML序列化和反序列化,我必须使用多个实例XmlArrayItemAttribute,如http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayitemattribute%28v=vs中所述.0.80%29.aspx

您可以应用XmlArrayItemAttribute或XmlElementAttribute的多个实例来指定可以插入到数组中的对象类型.

这是我的代码:

[XmlArrayItem(Type = typeof(Transformations.EvaluateExpressionTransform))]
[XmlArrayItem(Type = typeof(Transformations.ExtractTextTransform))]
[XmlArrayItem(Type = typeof(Transformations.InsertTextTransform))]
[XmlArrayItem(Type = typeof(Transformations.MapTextTransform))]
[XmlArrayItem(Type = typeof(Transformations.ReplaceTextTransform))]
[XmlArrayItem(Type = typeof(Transformations.TextItem))]
[XmlArrayItem(ElementName = "Transformation")]
public List<Transformations.TransformationItem> transformations;
Run Code Online (Sandbox Code Playgroud)

问题是,当我使用反射来获取ElementName属性时GetCustomAttribute(),我得到了AmbiguousMatchException.

我怎么能解决这个问题ElementName呢?

c# reflection custom-attributes

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

TypedArray和可设置样式的属性:getIndexCount()vs.length()

我正在解析自定义属性,遇到了一些奇怪的事情。假设我的解析器看起来像这样:

final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.Item);
final int size = attributes.getIndexCount();

for(int i = 0; i < size; i++) {
   final int attr = attributes.getIndex(i);
   if(attr == R.styleable.Item_custom_attrib) {
      final int resourceId = attributes.getResourceId(attr, -1);
      if(resourceId == -1)
         throw new Resources.NotFoundException("The resource specified was not found.");
...
}
attributes.recycle();
Run Code Online (Sandbox Code Playgroud)

这可行。现在,如果我将第2行替换为final int size = attributes.length();这意味着我得到了:

final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.Item);
final int size = attributes.length();

for(int i = 0; i < size; i++) {
   final int attr …
Run Code Online (Sandbox Code Playgroud)

android custom-attributes

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

C#属性或属性

我知道你可以像这样在C#中为方法添加一个属性,

EX1.

[HttpPost]
public void Method()
{
//code
}
Run Code Online (Sandbox Code Playgroud)

这意味着必须满足该属性才能运行Method().

我知道你可以堆叠这样的属性,

EX2.

[HttpPost]
[RequireHttps]
public void Method2()
{
 //More code
}
Run Code Online (Sandbox Code Playgroud)

在您可以使用之前检查是否满足attribute1'AND'attribute2 Method2().

但你能'或'属性吗?这样的事可能吗?

EX3.

[HttpPost || RequireHttps]
public void Method3()
{
  //Even more code
}
Run Code Online (Sandbox Code Playgroud)

因此,如果满足任一属性,您可以使用Method3().

编辑:对不起,印象属于名为Annotations的属性.修正了.

c# attributes annotations custom-attributes asp.net-mvc-3

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

如何通过自定义属性选择一些类的属性

我有一个3级的财产.

class Issuance
{
    [MyAttr]
    virtual public long Code1 { get; set; }

    [MyAttr]
    virtual public long Code2 { get; set; }

    virtual public long Code3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我需要通过自定义属性([MyAttr])选择此类中的一些属性.

我使用GetProperties()但是这会返回所有属性.

var myList = new Issuance().GetType().GetProperties();
//Count of result is 3 (Code1,Code2,Code3) But count of expected is 2(Code1,Code2) 
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

c# reflection attributes custom-attributes

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

属性构造函数不能使用console.writeline

所以我很好奇如果我将CallerMemberName属性应用于Attribute的构造函数的参数会发生什么.这是我非常简单的代码.

class MyAttribute : Attribute
{
    public MyAttribute(string message, [CallerMemberName] string name = "none specified")
    {
        Console.WriteLine("\{message}: \{name}");
    }
}
[My("Program")]
class Program
{
    [My("Main")]
    static void Main(string[] args)
    {
        var MyAttribute = new MyAttribute("Inner");
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

我运行代码,什么也没发生.所以我在构造函数中放置了一个断点,看它是否会被击中.确实如此.但是,即使Writeline调用似乎正在执行,也没有任何东西出现.为什么不?

此外,断点没有被[My("Program")]属性命中.为什么不?

编辑:根据SLaks的回答,有一个竞争条件对该计划可能是致命的.我将构造函数的内部更改为:

new Thread(() =>
{
    Thread.Sleep(1000);
    Console.WriteLine("\{message}: \{name}");
}).Start();
Run Code Online (Sandbox Code Playgroud)

Main和Inner属性以这种方式打印,但Program属性不打印.这是为什么?

c# custom-attributes

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

当类级别验证已在 MVC 中声明时,如何使用操作级别验证覆盖自定义过滤器属性

我的控制器在类级别使用一个属性,它只允许一个角色访问。这个控制器有 20 多个动作。但是对于仅一项操作,我还需要一个角色才能获得访问权限。我已经在类级别声明了属性过滤器,以便它对控制器类中的所有操作都能正常工作。但现在我只想在同一个控制器中覆盖一个动作。有没有可能?我正在使用 .Net 4.5 版。

过滤器属性实现是这样的:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class RequireModulePermissionAttribute : AuthorizeAttribute
{
   //code goes here
}
Run Code Online (Sandbox Code Playgroud)

控制器类:

[RequireModulePermission("Admin")]
public class AdministrationController : Controller
{
    [HttpPost]
    [RequireModulePermission("Admin","Supervisor")]
    public ActionResult CreateUser(UserViewModel userVM)
    {
       //code goes here
    }   
}
Run Code Online (Sandbox Code Playgroud)

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

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

通过自定义属性获取枚举(通用)

有很多示例可以通过自定义属性获取Enum,例如此处 从描述属性获取枚举

public static class EnumEx
{
    public static T GetValueFromDescription<T>(string description)
    {
        var type = typeof(T);
        if(!type.IsEnum) throw new InvalidOperationException();
        foreach(var field in type.GetFields())
        {
            var attribute = Attribute.GetCustomAttribute(field,
                typeof(DescriptionAttribute)) as DescriptionAttribute;
            if(attribute != null)
            {
                if(attribute.Description == description)
                    return (T)field.GetValue(null);
            }
            else
            {
                if(field.Name == description)
                    return (T)field.GetValue(null);
            }
        }
        throw new ArgumentException("Not found.", "description");
        // or return default(T);
    }
}
Run Code Online (Sandbox Code Playgroud)

但问题是你必须硬编码attributeType即 typeof(DescriptionAttribute)) as DescriptionAttribute

我如何将此示例转换为通用扩展,以便我不必硬编码CustomAttributeType.

c# generics enums custom-attributes

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

在c#中调用方法时如何执行属性

我有一个方法.

[AppAuthorize]
public ActionResult StolenCar(object claimContext)
{
    return View("StolenCar");
}
Run Code Online (Sandbox Code Playgroud)

我的属性是这样的代码.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class AppAuthorize : Vairs.Presentation.AppAuthorizationAttribute
{
    public bool ConditionalAuthentication
    {
        get;
        set;
    }


    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        bool requireAuthentication = true;

        if (ConditionalAuthentication)
        {

        }

        if (requireAuthentication)
        {
            var appContext = SecurityContext.Current as SitecoreSecurityContext;

            if (appContext != null)
            {
                appContext.LoginPage =  ConfigurationManager.AppSettings[SecurityPaths.Login];
                appContext.LogoffPage = ConfigurationManager.AppSettings[SecurityPaths.Logout];
            }

            if ((appContext != null) && this.RedirectToLogin && !string.IsNullOrEmpty(appContext.LoginPage))
            {
                appContext.RedirectToLogin = true; …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc custom-attributes system.reflection

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

React Typescript-添加自定义属性

React Typescript允许添加自定义data- *属性。但是是否可以添加自定义属性,例如'name'|| “测试”行为。?

<span name="I'm causing a type error" data-test="I'm Working"/>
Run Code Online (Sandbox Code Playgroud)

我加粗了。

类型错误:类型'{儿童:元素;名称:字符串;数据测试:字符串;}”不能分配给“ DetailedHTMLProps,HTMLSpanElement>”类型。 属性“名称”在类型 “ DetailedHTMLProps,HTMLSpanElement>” 上不存在。TS232

"react": "^16.7.0",
"typescript": "^3.2.4",
Run Code Online (Sandbox Code Playgroud)

custom-attributes typescript reactjs

1
推荐指数
3
解决办法
1501
查看次数