我有以下自定义属性,可以应用于属性:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IdentifierAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
例如:
public class MyClass
{
[Identifier()]
public string Name { get; set; }
public int SomeNumber { get; set; }
public string SomeOtherProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
还有其他类,Identifier属性可以添加到不同类型的属性中:
public class MyOtherClass
{
public string Name { get; set; }
[Identifier()]
public int SomeNumber { get; set; }
public string SomeOtherProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,我需要能够在我的消费类中获取此信息.例如:
public class TestClass<T>
{
public void GetIDForPassedInObject(T obj)
{
var type …Run Code Online (Sandbox Code Playgroud) 此问题已解决,请参阅注释以获取详细信息.
我正在扩展现有的Android View并加载一些自定义属性,如使用XML声明自定义Android UI元素和定义自定义attrs中所述.
具有布尔和整数格式的属性可以正常工作,但是当我尝试指定对数组资源的引用时,应用程序在启动时崩溃.我在xml资源文件中定义了一个整数数组,我试图将它用作自定义视图的属性.
我可以使用数组资源来设置android Spinner类的"entries"属性而没有错误,所以它似乎是我的实现中的一个问题.logcat消息似乎没有提供有关崩溃的任何具体信息,但我仍然在寻找,所以如果我找到了什么,我会更新.
属性由(在attrs.xml中)声明:
<declare-styleable name="CustomView">
<attr name="values" format="reference"/>
<attr name="isActive" format="boolean"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
该数组定义为(在arrays.xml中):
<integer-array name="nums">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
Run Code Online (Sandbox Code Playgroud)
我通过以下方式引用数组:
<com.test.CustomView cv:values="@array/nums" />
Run Code Online (Sandbox Code Playgroud)
这会导致应用程序立即崩溃.此外,如果我引用颜色资源而不是数组,那么应用程序不会崩溃.有人知道如何处理这个问题吗?
我想在登录表单中添加一些iOS特定的标记属性.如果我查看我的网页源代码,那么属性autocorrect,autocapitalize和spellcheck就不存在了.这是什么原因?我正在使用JSF 2.x.
<h:inputText id="user-name" forceId="true" value="#{login.username}" style="width:120px;"
autocorrect="off" autocapitalize="off" spellcheck="false" />
Run Code Online (Sandbox Code Playgroud) 有没有办法在EF生成的代码中为属性添加自定义属性?我唯一可以看到的是一个合理的解决方案是提出一个自定义的T4模板.但是,由于属性的性质,不可能为每个EF属性确定正确的属性参数.
是否有一种优雅的方法来获取具有自定义属性的程序集中的所有类型?
所以如果我有课
[Findable]
public class MyFindableClass
{}
Run Code Online (Sandbox Code Playgroud)
我希望能够在Assembly.GetTypes(...)返回的类型集合中找到它.
我可以用一个卑鄙的黑客来做,但我确信有人有更好的方式.
我在XML中定义了一个枚举的自定义属性.它看起来像这样:
<declare-styleable name="MyControl">
<attr name="myProperty">
<enum name="None" value="0"/>
<enum name="One" value="1"/>
<enum name="Two" value="2"/>
<enum name="Three" value="3"/>
<enum name="Four" value="4"/>
<enum name="Five" value="5"/>
<enum name="Six" value="6"/>
<enum name="Seven" value="7"/>
<enum name="Eight" value="8"/>
<enum name="Nine" value="9"/>
<enum name="Ten" value="10"/>
</attr>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
现在假设我想要另一个不相关的类来使用同一组枚举值.有没有办法做到这一点,而无需在新节点中制作新的枚举列表的副本?例如,具有以下类似语义的东西:
<declare-styleable name="MyUnrelatedControl">
<attr name="myProperty" format="[myEnum Format Reference]"/>
</declare-stylable>
Run Code Online (Sandbox Code Playgroud) 我正在使用反射来加载具有项目类结构的树视图.类中的每个成员都具有分配给它们的自定义属性.
获取类的属性我没有问题,MemberInfo.GetCustomAttributes()但是如果类成员是自定义类,然后需要解析自身以返回自定义属性,我需要一种方法.
到目前为止,我的代码是:
MemberInfo[] membersInfo = typeof(Project).GetProperties();
foreach (MemberInfo memberInfo in membersInfo)
{
foreach (object attribute in memberInfo.GetCustomAttributes(true))
{
// Get the custom attribute of the class and store on the treeview
if (attribute is ReportAttribute)
{
if (((ReportAttribute)attribute).FriendlyName.Length > 0)
{
treeItem.Items.Add(new TreeViewItem() { Header = ((ReportAttribute)attribute).FriendlyName });
}
}
// PROBLEM HERE : I need to work out if the object is a specific type
// and then use reflection to get the structure and attributes. …Run Code Online (Sandbox Code Playgroud) 假设我有一个名为Test的类,其中一个名为Title的属性带有自定义属性:
public class Test
{
[DatabaseField("title")]
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
还有一个名为DbField的扩展方法.我想知道是否可以在c#中从对象实例获取自定义属性.
Test t = new Test();
string fieldName = t.Title.DbField();
//fieldName will equal "title", the same name passed into the attribute above
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
我有一个控制器,只应在加载特定参数时请求授权.例如,当参数ID为8时.
我想到了使用这样的自定义验证属性:
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (/* Action's inputparameter ID = 8 */)
{
return base.AuthorizeCore(httpContext);
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我的动作看起来像这样(不是很有趣)
[MyAuthorize]
public ActionResult Protected(int id)
{
/* custom logic for setting the viewmodel from the id parameter */
return View(viewmodel);
}
Run Code Online (Sandbox Code Playgroud)
问题是您可以看到我不知道如何在authorize属性中检查该ID参数.你能帮我解决一下吗?
我有自定义属性,如下所示:
[AttributeUsage(AttributeTargets.Field)]
public class EnumDisplayAttribute : Attribute
{
public string Description { get; private set; }
public string Code { get; private set; }
public EnumDisplayAttribute(string description = null, string code = null)
{
Description = description;
Code = code;
}
}
Run Code Online (Sandbox Code Playgroud)
两个构造函数参数都是可选的.
在像这样的字段上使用此属性时
public enum TransactionType
{
[EnumDisplay(code: "B")]
Bill,
[EnumDisplay(description: null, code: "C")]
CashReceipt,
}
Run Code Online (Sandbox Code Playgroud)
我没有在代码编辑器中看到任何波形,但我看到一个模糊的错误,没有任何文件行号列.错误消息是:
错误CS0182:属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式
单击该错误不会执行任何操作.也就是说,您不会导航到错误站点(显然,因为没有行号和列).
即使我像这样设置属性:
[EnumDisplay("This is a Bill")]
Run Code Online (Sandbox Code Playgroud)
编译器不喜欢它.
实际上,我被迫提供两个参数(已命名或未命名),以便将此属性用作属性.
当然,如果我将此属性用作常规类,如下所示:
var enumDisplayAttribute = new EnumDisplayAttribute();
enumDisplayAttribute = new EnumDisplayAttribute(description: "This is a …Run Code Online (Sandbox Code Playgroud) c# ×5
.net ×3
reflection ×3
android ×2
android-xml ×1
attributes ×1
enums ×1
html ×1
jsf ×1
renderer ×1