我有多个带有自定义属性的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)
但无法让它发挥作用.任何帮助,将不胜感激.
我有一个List<TransformationItem>.TransformationItem只是多个类的基类,例如ExtractTextTransform和InsertTextTransform
为了使用内置的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呢?
我正在解析自定义属性,遇到了一些奇怪的事情。假设我的解析器看起来像这样:
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) 我知道你可以像这样在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的属性.修正了.
我有一个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)
我该怎么做?
所以我很好奇如果我将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属性不打印.这是为什么?
我的控制器在类级别使用一个属性,它只允许一个角色访问。这个控制器有 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) 有很多示例可以通过自定义属性获取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.
我有一个方法.
[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) 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) c# ×6
asp.net-mvc ×2
attributes ×2
reflection ×2
.net ×1
android ×1
annotations ×1
asp.net ×1
enums ×1
filter ×1
generics ×1
jquery ×1
jquery-ui ×1
reactjs ×1
typescript ×1