reflection

How do I sort a generic list based on a custom attribute?

空扰寡人 提交于 2020-01-13 10:10:27
问题 I am working in c#.NEt 2.0. I have a class, let's say X with many properties. Each properties has a custom attribute, an interger, which I planned to use to specify its order int he final array. Using reflection I read through all of the properties and group the values and place them into a generic list of properties. This works and I can grab the values. But the plan was SORT the list, based on the custom attribute placed on each property, and finally read out the propery values, already

How do I sort a generic list based on a custom attribute?

情到浓时终转凉″ 提交于 2020-01-13 10:09:23
问题 I am working in c#.NEt 2.0. I have a class, let's say X with many properties. Each properties has a custom attribute, an interger, which I planned to use to specify its order int he final array. Using reflection I read through all of the properties and group the values and place them into a generic list of properties. This works and I can grab the values. But the plan was SORT the list, based on the custom attribute placed on each property, and finally read out the propery values, already

Find all classes with an attribute containing a specific property value

蓝咒 提交于 2020-01-13 09:46:08
问题 Is it possible to find a class that is tagged with a custom attribute based on a value given to that attribute? Basically, I have classes that look like this - [MyAttr("CODE")] public class MyClass() {} From there I'm getting all the classes (Types) - var c = Assembly.GetExecutingAssembly().GetTypes().Where ( t => t.IsClass && t.Namespace == (typeof(AbstractParentClass)).Namespace && t.IsSubclassOf(typeof(AbstractParentClass)) ); This all appears to work. c contains all of the appropriate

Advantage of using CustomAttributes vs GetCustomAttributes()

北城余情 提交于 2020-01-13 09:34:11
问题 I noticed today that some new properties had appeared in my intellisense on the System.Type object for my .NET 4.5 projects. Among these was one called CustomAttributes . I was intrigued by this since I previously had understood that GetCustomAttributes was one of the most expensive reflection calls ( DynamicInvoke and the like aside, of course). As I understand it, every call to GetCustomAttributes results in calling the constructors for the attributes (and thus a memory allocation). I've

Java - Get reference to a static class using reflection

大城市里の小女人 提交于 2020-01-13 08:36:10
问题 In Java, is it possible to access an instance of a static class (nested) using reflection? Supposing I have the following 2 classes defined in the package Package1.SubPackage.SubSubPackage: public class MyMainClass { public static class SalesObjectGrouper1 { public static final GrouperContext CONTEXT = new GrouperContext("MyDate"); } private static class SalesObjectGrouper2 { public static final GrouperContext CONTEXT = new GrouperContext("MyDate"); } } If I run the following code: try { xyz

How to get the annotations of a method in Scala 2.11

大憨熊 提交于 2020-01-13 07:08:09
问题 Let's assume a controller object like this: object Users extends Controller { ... @ApiOperation( httpMethod = "POST", nickname = "authenticate", value = "Authenticates an user", notes = "Returns the JSON Web Token to be used in any subsequent request", response = classOf[models.auth.api.Jwt]) def authenticate = SecuredAction[Users.type]("authenticate").async(parse.json) { implicit request => ... } ... } How do I get the annotation values of the authenticate method at runtime? I've tried this:

convert JSON to contentvalues

北城余情 提交于 2020-01-13 06:42:07
问题 Is there an easy way to get JSON from a webservice and put it into my SQLite DB in C# mono for android (xamarin)? There are some tedious ways to do it but I want something quick and elegant. 回答1: The question is old, but here is the equivalent code of @gghuffer in Java: public static ContentValues objectToContentValues(Object o) throws IllegalAccessException { ContentValues cv = new ContentValues(); for (Field field : o.getClass().getFields()) { Object value = field.get(o); //check if

How can I access an instance field in an abstract parent class via reflection?

允我心安 提交于 2020-01-13 05:46:07
问题 So, for example, StringBuilder inherits from the abstract class AbstractStringBuilder . As I understand it, StringBuilder has no fields itself (except for serialVersionUID ). Rather, its state is represented by the fields in AbstractStringBuilder and manipulated by calling super in the implementations of the methods it overrides. Is there a way via reflection to get the private char array named value declared in AbstractStringBuilder that is associated with a particular instance of

Creating a function that converts functions of one type to another

半腔热情 提交于 2020-01-13 04:47:29
问题 For some fancy reflection stuff, I've got a function of type Func and need to pass it into a function that accepts type Func where T is not known until run time. For example: public bool MyOperation(Func<string,bool> op) { return _myValues.Any(op); } public static bool InvokeOperationMethod(MethodInfo info, object obj,Func<object,bool> opAsObject) { info.Invoke(obj, opAsObject); } The issue is that since I've got a lambda of a weaker type, I can't pass it as an argument of a stronger type. So

Why does accessing a parameter variable's attributes with Get-Variable only work the first time in the ISE?

ⅰ亾dé卋堺 提交于 2020-01-13 03:59:08
问题 Thanks to the great people at StackOverflow we received a very good answer on how to retrieve the values defined in ValidateSet within the Param() clause of a script or function: Param ( [ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')] [String]$Type = 'Startup' ) (Get-Variable Type).Attributes.ValidValues The only thing that bothers me is that this code only works the first time when you run it in the PowerShell ISE. The second time you run it, there is no output generated. Is there a