我尝试使用下面的代码创建自定义.NET属性,但不小心将子类留下了.这会在注释中生成一个容易修复的编译器错误.
// results in compiler error CS0641: Attribute 'AttributeUsage' is
// only valid on classes derived from System.Attribute
[AttributeUsage(AttributeTargets.Class)]
internal class ToolDeclarationAttribute
{
internal ToolDeclarationAttribute()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是编译器如何知道[AttributeUsage]属性只能应用于子类System.Attribute?使用.NET Reflector我没有看到AttributeUsageAttribute类声明本身有什么特别之处.不幸的是,这可能只是编译器本身生成的一种特殊情况.
[Serializable, ComVisible(true), AttributeUsage(AttributeTargets.Class, Inherited=true)]
public sealed class AttributeUsageAttribute : Attribute
{
...
Run Code Online (Sandbox Code Playgroud)
我希望能够指定我的自定义属性只能放在特定类(或接口)的子类上.这可能吗?
我的asp.net mvc(C#)应用程序中有两个自定义属性.
[CustAttribute1()]
[CustAttribute2()]
Run Code Online (Sandbox Code Playgroud)
当我将这些属性用于我的行为时,会先执行哪些属性?
[CustAttribute1()]
[CustAttribute2()]
public ActionResult Index()
{
Run Code Online (Sandbox Code Playgroud)
我可以为我的操作使用多个自定义属性吗?如果是这样,在上面的Action中,首先会执行哪个自定义属性?
如果我创建了一个属性:
public class TableAttribute : Attribute {
public string HeaderText { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在课堂上申请了一些我的房产
public class Person {
[Table(HeaderText="F. Name")]
public string FirstName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的视图中,我有一个显示在表格中的人员列表..如何检索HeaderText的值以用作我的列标题?就像是...
<th><%:HeaderText%></th>
Run Code Online (Sandbox Code Playgroud) 我想,以确保两个接口都从不在编译时相同的类,类似于如何发现AttributeUsage检查定制在编译时属性.
例如:
[InterfaceUsage(MutuallyExclusive = typeof(B))]
interface A {
//...
}
interface B {
//...
}
class C : A, B { //should throw an error on compile time
//...
}
Run Code Online (Sandbox Code Playgroud)
我显然可以在运行时使用反射来完成此操作,但我对编译时解决方案很感兴趣.
我想象一个可能不存在开箱即用 - 但有没有办法创建一个在编译时运行的自定义属性,就像AttributeUsage一样?
什么是在方法重载时从类方法和接口方法获取属性值的最佳方法?
例如,我想知道在下面的示例中,带有一个参数的Get方法具有两个属性,值为5和"any",而另一个方法具有值为7和"private"的属性.
public class ScopeAttribute : System.Attribute
{
public string Allowed { get; set; }
}
public class SizeAttribute : System.Attribute
{
public int Max { get; set; }
}
public interface Interface1
{
[SizeAttribute( Max = 5 )]
string Get( string name );
[SizeAttribute( Max = 7 )]
string Get( string name, string area );
}
public class Class1 : Interface1
{
[ScopeAttribute( Allowed = "any" )]
public string Get( string name )
{
return string.Empty;
}
[ScopeAttribute( Allowed …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Metro Style App便携式库中的类上定义和检索自定义属性.
就像是
[AttributeUsage(AttributeTargets.Class)]
public class FooAttribute : Attribute
{
}
[Foo]
public class Bar
{
}
class Program
{
static void Main(string[] args)
{
var attrs = CustomAttributeExtensions.GetCustomAttribute<FooAttribute>(typeof(Bar));
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于普通的4.5,但在一个便携式图书馆中,它针对地铁风格的应用程序,它告诉我
Cannot convert type 'System.Type' to 'System.Reflection.MemberInfo'
Run Code Online (Sandbox Code Playgroud)
谢谢
c# custom-attributes system.reflection portable-class-library windows-runtime
有人知道任何代码重复DebuggerDisplayAttribute分析和收集结果字符串的方式吗?
我想创建一个几乎完成示例事务的自定义属性.类似于"当遇到断点时...",您可以在花括号中使用变量,如"{variable}"中所示.
我已经处理过简单的情况,例如"{Name}",但像"{Foo.Name}"这样的东西需要我需要帮助的额外反射代码.
基本上,我想使用DebuggerDisplayAttribute文档中定义的规则解析字符串.目前,我可以解析并解决"我是{GetName()}".我需要帮助"Foo的名字:{Foo.Name}"
我一直在努力寻找如何编写自定义属性来验证方法参数的示例,即转换此表单:
public void DoSomething(Client client)
{
if (client.HasAction("do_something"))
{
// ...
}
else
{
throw new RequiredActionException(client, "do_something");
}
}
Run Code Online (Sandbox Code Playgroud)
进入这个:
public void DoSomething([RequiredAction(Action="some_action")] Client client)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
据我所知,我需要将此属性添加到我的自定义属性,但我对如何访问修饰参数感到茫然Client:
[AttributeUsageAttribute(AttributeTargets.Parameter)]
public class RequireActionAttribute : System.Attribute
{
public Type Action {get; set;}
public RequireActionAttribute()
{
// .. How do you access the decorated parameter?
Client client = ???
if (!client.HasAction(Action))
{
throw new RequiredActionException(client, Action);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在玩Aurelia中的自定义元素功能,并试图创建一个'进度条'元素.
进步,bar.js
import {customElement, bindable} from 'aurelia-framework';
@customElement('progress-bar')
export class ProgressBar
{
//do stuff//
}
Run Code Online (Sandbox Code Playgroud)
进步,一个bar.html
<template>
<link rel="stylesheet" type="text/css" href="src/components/progress-bar.css">
<div class='progress-bar' tabindex=0 ref="bar">bloo</div>
</template>
Run Code Online (Sandbox Code Playgroud)
test.html(相关部分)
<require from="./components/progress-bar"></require>
<progress-bar ref="pb">a</progress-bar>
Run Code Online (Sandbox Code Playgroud)
所有这一切都很好.但我正在努力如何让主页可以调用某个函数或更改元素上的某些属性,这应该在进度条本身上做一些事情.
我试图在progress-bar.js中创建一个函数'doSomething',但是我无法在test.js中访问它.
添加到progress-bar.js
doSomething(percent, value) {
$(this.bar).animate('width: ${percent}%', speed);
}
Run Code Online (Sandbox Code Playgroud)
在test.js里面
clicked() {
console.log(this.pb); // this returns the progress bar element fine
console.log(this.pb.doSomething); //this returns as undefined
this.pb.doSomething(percent, value); // this fails outright with error: typeError - doSomething is not a function
}
Run Code Online (Sandbox Code Playgroud)
接下来,我尝试在progress-bar元素中设置自定义属性,并可能使用valueChange来改变div.
里面的进度bar.js
@bindable percent=null;
@bindable speed=null; …Run Code Online (Sandbox Code Playgroud) 我发现使用AMP需要以下CSS样板代码.它有什么作用?
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>Run Code Online (Sandbox Code Playgroud)
任何人都可以知道在AMP页面中包含上述css代码的原因吗?
和
我可以写< style amp-boilerplate="amp-boilerplate">而不是< style amp-boilerplate>吗?
c# ×7
.net ×3
attributes ×2
asp.net-mvc ×1
aurelia ×1
compile-time ×1
css ×1
decorator ×1
html ×1
javascript ×1
reflection ×1