custom-attributes

Why serializing Version with JsonPropertyAttribute doesn't work?

♀尐吖头ヾ 提交于 2019-11-27 08:36:48
问题 I am trying to serialize an object with a static System.Version field: [JsonObject(MemberSerialization.OptIn)] public class MyObj { [JsonProperty] private static string testStr; [JsonProperty(ItemConverterType = typeof(VersionConverter))] private static Version ver = System.Reflection.Assembly...Version; // some other non-serialized fields // ... } I have learnt from this question that Version needs a custom converter, which I added as ItemConverterType . However, when I try to serialize it

Custom attribute on property - Getting type and value of attributed property

三世轮回 提交于 2019-11-27 07:15:45
I have the following custom attribute, which can be applied on properties: [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class IdentifierAttribute : Attribute { } For example: public class MyClass { [Identifier()] public string Name { get; set; } public int SomeNumber { get; set; } public string SomeOtherProperty { get; set; } } There will also be other classes, to which the Identifier attribute could be added to properties of different type: public class MyOtherClass { public string Name { get; set; } [Identifier()] public int SomeNumber { get; set; } public string

Custom attributes in Android fragments

人走茶凉 提交于 2019-11-27 07:13:55
I'd like to define custom attributes in Android fragment using XML (without using bundle additional parameters) like declare-styleable in custom controls. But there are no constructors with AttrSet parameters, so is it possible? Can i just override public void onInflate(android.app.Activity activity, android.util.AttributeSet attrs, android.os.Bundle savedInstanceState) in order to get attributes support? The Link for Support4Demos is changed or can be changed so posting the complete solution. Here it goes. Create attrs.xml file in res/values folder. Or add the below content if file already

Lambda expression in attribute constructor

非 Y 不嫁゛ 提交于 2019-11-27 05:32:04
问题 I have created an Attribute class called RelatedPropertyAttribute : [AttributeUsage(AttributeTargets.Property)] public class RelatedPropertyAttribute: Attribute { public string RelatedProperty { get; private set; } public RelatedPropertyAttribute(string relatedProperty) { RelatedProperty = relatedProperty; } } I use this to indicate related properties in a class. Example of how I would use it: public class MyClass { public int EmployeeID { get; set; } [RelatedProperty("EmployeeID")] public

WCF service attribute to log method calls and exceptions

最后都变了- 提交于 2019-11-27 03:52:12
I have a requirement to log each method call in a WCF service, and any exceptions thrown. This has led to a lot of redundant code, because each method needs to include boilerplate similar to this: [OperationContract] public ResultBase<int> Add(int x, int y) { var parameters = new object[] { x, y } MyInfrastructure.LogStart("Add", parameters); try { // actual method body goes here } catch (Exception ex) { MyInfrastructure.LogError("Add", parameters, ex); return new ResultBase<int>("Oops, the request failed", ex); } MyInfrastructure.LogEnd("Add", parameters); } Is there a way I can encapsulate

Get a List<string> of my enum attributes with a generic method

冷暖自知 提交于 2019-11-27 03:35:44
问题 At start, we have this basic enum. public enum E_Levels { [ValueOfEnum("Low level")] LOW, [ValueOfEnum("Normal level")] NORMAL, [ValueOfEnum("High level")] HIGH } And I would like to get a List<string> whatever the enum . Something like Extensions.GetValuesOfEnum<E_Levels>() which could return a List<string> with "Low level", "Normal level" and "High level" in it. StackOF helped me to get one value attribute : public static class Extensions { public static string ToValueOfEnum(this Enum value

How to pass custom component parameters in java and xml

烈酒焚心 提交于 2019-11-27 02:52:32
When creating a custom component in android it is often asked how to create and pass through the attrs property to the constructor. It is often suggested that when creating a component in java that you simply use the default constructor, i.e. new MyComponent(context); rather than attempting to create an attrs object to pass through to the overloaded constructor often seen in xml based custom components. I've tried to create an attrs object and it doesn't seem either easy or at all possible (without an exceedingly complicated process), and by all accounts isn't really required. My question is

Attribute Constructor With Lambda

纵饮孤独 提交于 2019-11-27 02:19:47
问题 It is possible to do this: public static void SomeMethod<TFunc>(Expression<TFunc> expr) { //LambdaExpression happily excepts any Expession<TFunc> LambdaExpression lamb = expr; } and call it elsewhere passing a lambda for the parameter: SomeMethod<Func<IQueryable<Person>,Person>>( p=>p.FirstOrDefault()); I would instead like to pass an expression as a parameter to an attribute constructor . Is it possible to do the below? class ExpandableQueryAttribute: Attribute { private LambdaExpression

How can I add a custom root node when serializing an object with JSON.NET?

孤者浪人 提交于 2019-11-27 01:53:53
问题 I have added a custom property to some of my objects like this: [JsonCustomRoot("status")] public class StatusDTO { public int StatusId { get; set; } public string Name { get; set; } public DateTime Created { get; set; } } The attribute is very simple: public class JsonCustomRoot :Attribute { public string rootName { get; set; } public JsonCustomRoot(string rootName) { this.rootName = rootName; } } The default output from JSON.NET when serializing an instance of an object is this: {"StatusId"

Generating a custom compile time warning C#

末鹿安然 提交于 2019-11-27 01:20:47
问题 I'm using VS2008 and would like to create a compile time warning / error based on custom attributes on a property (if it is possible). There are two cases which interest me currently: [MyAttribute (typeof(MyClass)] Where MyClass has to implement an interface. Currently I assert this in the constructor of the attribute, however this doesn't make it easy to track down, due to the nature of the stack trace: public MyAttribute (Type MyClassType) { System.Diagnostics.Debug.Assert(typeof