Is it possible to have a delegate as attribute parameter?

No, you cannot have a delegate as an attribute constructor parameter. See available types: Attribute parameter types As a workaround (although it’s hacky and error prone) you can create a delegate with reflection: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false, Inherited = true)] public class WorkspaceAttribute : Attribute { public ConnectionPropertiesDelegate ConnectionDelegate { get; set; } … Read more

How to pass custom component parameters in java and xml

(Full disclosure: This question is an offshoot of Creating custom view) You can create constructors beyond the three standard ones inherited from View that add the attributes you want… MyComponent(Context context, String foo) { super(context); // Do something with foo } …but I don’t recommend it. It’s better to follow the same convention as other … Read more

How to get Custom Attribute values for enums?

Try using a generic method Attribute: class DayAttribute : Attribute { public string Name { get; private set; } public DayAttribute(string name) { this.Name = name; } } Enum: enum Days { [Day(“Saturday”)] Sat, [Day(“Sunday”)] Sun, [Day(“Monday”)] Mon, [Day(“Tuesday”)] Tue, [Day(“Wednesday”)] Wed, [Day(“Thursday”)] Thu, [Day(“Friday”)] Fri } Generic method: public static TAttribute GetAttribute<TAttribute>(this Enum value) … Read more

jquery: get value of custom attribute

You need some form of iteration here, as val (except when called with a function) only works on the first element: $(“input[placeholder]”).val($(“input[placeholder]”).attr(“placeholder”)); should be: $(“input[placeholder]”).each( function () { $(this).val( $(this).attr(“placeholder”) ); }); or $(“input[placeholder]”).val(function() { return $(this).attr(“placeholder”); });

Exclude property from serialization via custom attribute (json.net)

Here’s a generic reusable “ignore property” resolver based on the accepted answer: /// <summary> /// Special JsonConvert resolver that allows you to ignore properties. See https://stackoverflow.com/a/13588192/1037948 /// </summary> public class IgnorableSerializerContractResolver : DefaultContractResolver { protected readonly Dictionary<Type, HashSet<string>> Ignores; public IgnorableSerializerContractResolver() { this.Ignores = new Dictionary<Type, HashSet<string>>(); } /// <summary> /// Explicitly ignore the given … Read more

Find methods that have custom attribute using reflection

Your code is completely wrong. You are looping through every type that has the attribute, which will not find any types. You need to loop through every method on every type and check whether it has your attribute. For example: var methods = assembly.GetTypes() .SelectMany(t => t.GetMethods()) .Where(m => m.GetCustomAttributes(typeof(MenuItemAttribute), false).Length > 0) .ToArray();

Can I add custom methods/attributes to built-in Python types?

You can’t directly add the method to the original type. However, you can subclass the type then substitute it in the built-in/global namespace, which achieves most of the effect desired. Unfortunately, objects created by literal syntax will continue to be of the vanilla type and won’t have your new methods/attributes. Here’s what it looks like … Read more

ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

I could do this with a custom attribute as follows. [AuthorizeUser(AccessLevel = “Create”)] public ActionResult CreateNewInvoice() { //… return View(); } Custom Attribute class as follows. public class AuthorizeUserAttribute : AuthorizeAttribute { // Custom property public string AccessLevel { get; set; } protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorized = base.AuthorizeCore(httpContext); if (!isAuthorized) { … Read more

How to create a custom attribute in C#

You start by writing a class that derives from Attribute: public class MyCustomAttribute: Attribute { public string SomeProperty { get; set; } } Then you could decorate anything (class, method, property, …) with this attribute: [MyCustomAttribute(SomeProperty = “foo bar”)] public class Foo { } and finally you would use reflection to fetch it: var customAttributes … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)