reflection

How do you programmatically get a list of all common parameters?

陌路散爱 提交于 2020-01-01 09:01:07
问题 Powershell Cmdlets inherit a bunch of common parameters. Some cmdlets I write end up with predicates that depend on which parameters are actually bound. This often leads to filtering out common parameters which means you need a list of common parameter names. I also expect there to be difference in the list of common parameters from one version of powershell to another. All of this boils down to this question: How do you programmatically determine the list of common parameters? 回答1: What

Why don't Scala primitives show up as type parameters in Java reflection?

半世苍凉 提交于 2020-01-01 08:36:07
问题 Given the following case class: case class Foo( bar: Int, baz: Boolean, qux: Option[Int], quux: Option[Boolean], quuux: Option[Integer] ) I would expect the following: for (f <- classOf[Foo].getDeclaredFields) { println(f.getGenericType) } to produce something like: int boolean scala.Option<int> scala.Option<boolean> scala.Option<java.lang.Integer> But instead, it produces: int boolean scala.Option<java.lang.Object> scala.Option<java.lang.Object> scala.Option<java.lang.Integer> Why do the

Unable to cast object of type 'System.Object[]' to 'MyObject[]', what gives?

回眸只為那壹抹淺笑 提交于 2020-01-01 08:34:09
问题 Scenario: I'm currently writing a layer to abstract 3 similar webservices into one useable class. Each webservice exposes a set of objects that share commonality. I have created a set of intermediary objects which exploit the commonality. However in my layer I need to convert between the web service objects and my objects. I've used reflection to create the appropriate type at run time before I make the call to the web service like so: public static object[] CreateProperties(Type type,

How can I determine if an implicit cast exists in C#?

…衆ロ難τιáo~ 提交于 2020-01-01 07:31:08
问题 I have two types, T and U, and I want to know whether an implicit cast operator is defined from T to U. I'm aware of the existence of IsAssignableFrom, and this is not what I'm looking for, as it doesn't deal with implicit casts. A bit of googling led me to this solution, but in the author's own words this is ugly code (it tries to cast implicitly and returns false if there's an exception, true otherwise...) It seems testing for the existence of an op_Implicit method with the correct

How to find all direct subclasses of a class with .NET Reflection

烂漫一生 提交于 2020-01-01 07:30:10
问题 Consider the following classes hierarchy: base class A, classes B and C inherited from A and class D inherited from B. public class A {...} public class B : A {...} public class C : A {...} public class D : B {...} I can use following code to find all subclasses of A including D: var baseType = typeof(A); var assembly = typeof(A).Assembly; var types = assembly.GetTypes().Where(t => t.IsSubclassOf(baseType)); But I need to find only direct subclasses of A (B and C in example) and exclude all

How do I get a list of packages and/or classes on the classpath?

帅比萌擦擦* 提交于 2020-01-01 06:54:07
问题 In Java, I can use a ClassLoader to get a list of classes that are already loaded, and the packages of those classes. But how do I get a list of classes that could be loaded, i.e. are on the classpath? Same with packages. This is for a compiler; when parsing foo.bar.Baz, I want to know whether foo is a package to distinguish it from anything else. 回答1: Its a bit tricky and there are a few libraries that can help, but basically... Look at your classpath If you are dealing with a directory, you

TypeDescriptor.GetProperties vs. Type.GetProperties

╄→гoц情女王★ 提交于 2020-01-01 05:41:46
问题 I'm looking at some code where an MSDN author uses the following in different methods of the same class: if ( TypeDescriptor.GetProperties(ModelInstance)[propertyName] != null ) return; var property = ModelInstance.GetType().GetProperty(propertyName); Would you use the former because its faster and you only need to query a property and the latter if you need to manipulate it? Something else? 回答1: The first method should generally not be faster since internally per default it actually uses the

Recursive routine to obtain PropertyInfo

☆樱花仙子☆ 提交于 2020-01-01 05:33:07
问题 I'm attempting to create a recursive routine that will retrieve PropertyInfos for all members under a specified object (in .NET 3.5). Everything for immediate members is working, but it needs to parse nested classes as well (and their nested classes, etc). I do not understand how to handle the section that parses nested classes. How would you write this part of the code? public class ObjectWalkerEntity { public object Value { get; set; } public PropertyInfo PropertyInfo { get; set; } } public

See java annotations on calling method

只谈情不闲聊 提交于 2020-01-01 05:14:52
问题 Let's say I have a situation like this: public String method(String s) { return stringForThisVisibility(s, EnumVisibility.PUBLIC); } and I want to replace it with an annotation like this: @VisibilityLevel(value = EnumVisibility.PUBLIC) public String method(String s) { return stringForThisVisibility(s); } This seems to be a better and more clear solution, but i need for stringForThisVisibility method to know value of @VisibilityLevel with some kind of reflection. Is that possible? Can I see

Java: How to get a class object of the current class from a static context?

北城余情 提交于 2020-01-01 04:39:07
问题 I have a logging function that takes the calling object as a parameter. I then call getClass().getSimpleName() on it so that I can easily get the class name to add to my log entry for easy reference. The problem is that when I call my log function from a static method, I can't pass in "this". My log function looks something like this: public static void log(Object o, String msg){ do_log(o.getClass().getSimpleName()+" "+msg); } public void do_something(){ log(this, "Some message"); } But let's