Notify Developer of a “DO NOT USE” Method

Prior to VS2013 you could use: [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] so that it doesn’t show up in IntelliSense. If the consumer still wants to use it they can, but it won’t be as discoverable. Keith’s point about over-engineering still stands though. Since VS2013 this feature has been removed. As noted in https://github.com/dotnet/roslyn/issues/37478 this was “by design” and apparently … Read more

C# attribute text from resource file?

Here is my solution. I’ve added resourceName and resourceType properties to attribute, like microsoft has done in DataAnnotations. public class CustomAttribute : Attribute { public CustomAttribute(Type resourceType, string resourceName) { Message = ResourceHelper.GetResourceLookup(resourceType, resourceName); } public string Message { get; set; } } public class ResourceHelper { public static string GetResourceLookup(Type resourceType, string resourceName) { … Read more

SQL query to determine if a JSON value contains a specified attribute

I assume you’re using MySQL 5.7, which adds the JSON data type. Use JSON_EXTRACT(colname, ‘$.cost’) to access the cost property. It will be NULL is there’s no such property. WHERE JSON_EXTRACT(colname, ‘$.cost’) IS NOT NULL WHERE JSON_EXTRACT(colname, ‘$.cost’) IS NULL WHERE JSON_EXTRACT(colname, ‘$.cost’) != ” It will also be NULL if the value in the … Read more

Should I use “public” attributes or “public” properties in Python?

Typically, Python code strives to adhere to the Uniform Access Principle. Specifically, the accepted approach is: Expose your instance variables directly, allowing, for instance, foo.x = 0, not foo.set_x(0) If you need to wrap the accesses inside methods, for whatever reason, use @property, which preserves the access semantics. That is, foo.x = 0 now invokes … Read more

How to check if an object has an attribute?

Try hasattr(): if hasattr(a, ‘property’): a.property See zweiterlinde’s answer below, who offers good advice about asking forgiveness! A very pythonic approach! The general practice in python is that, if the property is likely to be there most of the time, simply call it and either let the exception propagate, or trap it with a try/except … Read more

Difference between methods and attributes in python

Terminology Mental model: A variable stored in an instance or class is called an attribute. A function stored in an instance or class is called a method. According to Python’s glossary: attribute: A value associated with an object which is referenced by name using dotted expressions. For example, if an object o has an attribute … Read more

Get enum from enum attribute

Here’s a helper method that should point you in the right direction. protected Als GetEnumByStringValueAttribute(string value) { Type enumType = typeof(Als); foreach (Enum val in Enum.GetValues(enumType)) { FieldInfo fi = enumType.GetField(val.ToString()); StringValueAttribute[] attributes = (StringValueAttribute[])fi.GetCustomAttributes( typeof(StringValueAttribute), false); StringValueAttribute attr = attributes[0]; if (attr.Value == value) { return (Als)val; } } throw new ArgumentException(“The value ‘” … Read more

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