custom-attributes

How to determine the attached type from within a custom attribute?

我只是一个虾纸丫 提交于 2019-12-01 17:56:47
问题 I have a custom attribute which can be assigned to a class, [FooAttribute] . What I would like to do, from within the attribute, is determine which type has actually used me. e.g. If I have: [FooAttribute] public class Bar { } In the code for FooAttribute, how can I determine it was Bar class that added me? I'm not specifically looking for the Bar type, I just want to set a friendly name using reflection. e.g. [FooAttribute(Name="MyFriendlyNameForThisClass")] public class Bar { } public class

Can you apply a attribute to multiple fields in C#?

会有一股神秘感。 提交于 2019-12-01 16:12:42
This doesn't seem possible, but I'll ask anyway... Is it possible in C# to apply a single attribute to multiple fields at once? public class MyClass { [SomeAttribute] public int m_nVar1; [SomeAttribute] public int m_nVar2; public int m_nVar3; } Is there a short-hand method to put the "SomeAttribute" on m_Var1 & m_Var2, but not on m_nVar3? Currently, we are placing the attributes before each field, but it would be nice to put all the fields using a attribute inside a block. Yes, it's possible: [SomeAttribute] public int m_nVar1, m_nVar2; (but obviously only if the types are the same) This could

Can you apply a attribute to multiple fields in C#?

☆樱花仙子☆ 提交于 2019-12-01 15:25:07
问题 This doesn't seem possible, but I'll ask anyway... Is it possible in C# to apply a single attribute to multiple fields at once? public class MyClass { [SomeAttribute] public int m_nVar1; [SomeAttribute] public int m_nVar2; public int m_nVar3; } Is there a short-hand method to put the "SomeAttribute" on m_Var1 & m_Var2, but not on m_nVar3? Currently, we are placing the attributes before each field, but it would be nice to put all the fields using a attribute inside a block. 回答1: Yes, it's

How to define named Parameters C#

妖精的绣舞 提交于 2019-12-01 02:24:36
This seems like a simple question, but for some reason I can't find the answer anywhere. Basically, I'd like to be able to implement a constructor that takes NamedParameters . By named parameters, I do not mean parameters with default values (optional parameters) such as: public SomeMethod(){ string newBar = Foo(bar2 : "customBar2"); } public string Foo(string bar1 = "bar1", bar2 = "bar2" ){ //... } A good example of what I'm trying to achieve is the AuthorizeAttribute from the System.Web.Mvc assembly. Which you can use the following way: [Authorize(Roles = "Administrators", Users =

Force attribute usage in subclass of abstract superclass

冷暖自知 提交于 2019-11-30 23:04:59
问题 How can I force a subclass to implement certain Attributes of its superclass? The reason is that I want to use Attributes for general information about the class, e.g. "DisplayName", "Description" or "Capabilities". So I thought I might implement them in a superclass and force the subclasses to implement the attributes. Is there something like an abstract attribute like for methods? [abstract DeclareMe] public abstract class InheritMe { public abstract void DeclareMe(); } 回答1: As your class

How to use method parameter attributes

ぐ巨炮叔叔 提交于 2019-11-30 18:39:56
I've been struggling to find examples of how to write a custom attribute to validate method parameters, i.e., turn this form: public void DoSomething(Client client) { if (client.HasAction("do_something")) { // ... } else { throw new RequiredActionException(client, "do_something"); } } into this: public void DoSomething([RequiredAction(Action="some_action")] Client client) { // ... } As far as I can tell, I need to add this attribute to my custom attribute, but I'm at a loss on how to access the decorated parameter Client : [AttributeUsageAttribute(AttributeTargets.Parameter)] public class

Is it a bad practice to add extra attributes to HTML elements?

久未见 提交于 2019-11-30 17:16:00
Sometimes I add an attribute to some of my controls. Like: <a href id="myLlink" isClimber="True">Chris Sharma</a> I know it is not a valid html. But it helps me in some cases. Is this considered as a bad practice? A friend of mine says that it is ok for Intranet environment but on internet it might not be find friendly by search engines. If it is not a good practice, what are the best practicess? Thanks Yes. It is considered a bad practice. Your HTML (if it's 4.0) won't validate successfully. Instead, add a class like so: <a href id="myLlink" class="climber" >...</a> Remember that you can have

.NET, C#: How to add a custom serialization attribute that acts as ISerializable interface

拟墨画扇 提交于 2019-11-30 15:03:21
I am doing some serialization of db linq objects, which contain EntitySet and EntityRef classes. I found a pretty easy way to deal with serialization of these classes, by simply using ISerializable to properly handle members of this type (converting them to lists for serialization, and undoing it on deserialization). However, it would be really nice if I could do: [Serializable] [SerializeLinqEntities] partial class Person { ... } Instead of: partial class Person : ISerializable { public virtual void GetObjectData( SerializationInfo si, StreamingContext ctxt ) { EntitySerializer.Serialize(this

ASP MVC C#: Is it possible to pass dynamic values into an attribute?

我的未来我决定 提交于 2019-11-30 14:09:38
Okay I'm very new to C# and i'm trying to create a little website using ASP MVC2. I want to create my own authorization attribute. but i need to pass some values if this is possible. For example: [CustomAuthorize(GroupID = Method Parameter?] public ActionResult DoSomething(int GroupID) { return View(""); } I want to authorize the access to a page. but it depends on the value passed to the controller. So the authorization depends on the groupID. Is this possible to achieve this in any way?. Thanks in advance. Use the value provider: public class CustomAuthorizeAttribute : FilterAttribute,

Retrieve custom attribute parameter values?

若如初见. 提交于 2019-11-30 13:52:38
if i have created an attribute: public class TableAttribute : Attribute { public string HeaderText { get; set; } } which i apply to a few of my properties in a class public class Person { [Table(HeaderText="F. Name")] public string FirstName { get; set; } } in my view i have a list of people which i am displaying in a table.. how can i retrieve the value of HeaderText to use as my column headers? Something like... <th><%:HeaderText%></th> In this case, you'd first retrieve the relevant PropertyInfo , then call MemberInfo.GetCustomAttributes (passing in your attribute type). Cast the result to