reflection

Type.GetInterfaces() for declared interfaces only

一曲冷凌霜 提交于 2020-01-03 13:05:09
问题 First of all, there are many questions just like this, and perhaps some OPs were even asking this exact same question. The problem is that no answer to those questions (accepted or not) actually answer this question, at least none that I can find. How can I determine the interfaces a class declares directly, and not those inherited either by parents nor by the interfaces declared? e.g. interface I {} interface W : I {} class C : W {} class D : C, I {} class E : D {} Results: C declares W D

reflection on List and printing values

限于喜欢 提交于 2020-01-03 11:45:32
问题 I wrote a method that accepts a generic parameter and then it prints its properties. I use it to test my web service. It's working but I want to add some features that I don't know how to implement. I want to print values of lists, because now it just writes System.Collection.Generic.List1 which is expected. Here is my code so far, this is working for basic types (int, double etc.): static void printReturnedProperties<T>(T Object) { PropertyInfo[] propertyInfos = null; propertyInfos = Object

How to retrieve name of a generic method, including generic types names

霸气de小男生 提交于 2020-01-03 09:28:43
问题 In C# , I have a method with the following signature : List<T> Load<T>(Repository<T> repository) Inside Load() method, i'd like to dump full method name (for debugging purposes), including the generic type. eg : calling Load<SomeRepository>(); would write "Load<SomeRepository>" What i have try so far : using MethodBase.GetCurrentMethod() and GetGenericArguments() to retrieve information. List<T> Load<T>(Repository<T> repository) { Debug.WriteLine(GetMethodName(MethodBase.GetCurrentMethod()));

Reflecting constant properties/fields in .net [duplicate]

ε祈祈猫儿з 提交于 2020-01-03 08:52:52
问题 This question already has answers here : Type.GetFields() - only returning “public const” fields (3 answers) Closed last year . I have a class which looks like as follows: public class MyConstants { public const int ONE = 1; public const int TWO = 2; Type thisObject; public MyConstants() { thisObject = this.GetType(); } public void EnumerateConstants() { PropertyInfo[] thisObjectProperties = thisObject.GetProperties(BindingFlags.Public); foreach (PropertyInfo info in thisObjectProperties) { /

How to get MethodInfo of a generic method on a non generic .NET type? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 08:50:12
问题 This question already has answers here : Is there a good way of getting MethodInfo of open generic method? (3 answers) Closed 6 years ago . I have this little problem, that I cannot figure out which arguments to pass to Type.GetMethod in order to get back the MethodInfo of a generic method on a non generic type. Specifically, I have this type definition: public static class A { public static B F<T>(bool dummy) { } public static B F<T>(IEnumerable<T> arg) { ... } } I have tried several takes

More than one method with the same parameter types in a class

半城伤御伤魂 提交于 2020-01-03 08:08:15
问题 I know, there is already at least one question on this topic. But I want to ask it again because this is what I discovered in the javadoc of Class#getDeclaredMethod(...): If more than one method with the same parameter types is declared in a class, and one of these methods has a return type that is more specific than any of the others, that method is returned; otherwise one of the methods is chosen arbitrarily. So the developers of the reflection in java consider that case as a probable one,

More than one method with the same parameter types in a class

人盡茶涼 提交于 2020-01-03 08:08:07
问题 I know, there is already at least one question on this topic. But I want to ask it again because this is what I discovered in the javadoc of Class#getDeclaredMethod(...): If more than one method with the same parameter types is declared in a class, and one of these methods has a return type that is more specific than any of the others, that method is returned; otherwise one of the methods is chosen arbitrarily. So the developers of the reflection in java consider that case as a probable one,

Type Casting an Object using a “Type” Object in C#

只谈情不闲聊 提交于 2020-01-03 07:26:09
问题 This one has proven to be a little tricky for me so far. I am wondering if it is possible to type cast an object using a System.Type object. I have illustrated below what I mean: public interface IDataAdapter { object Transform(object input); Type GetOutputType(); } public class SomeRandomAdapter : IDataAdapter { public object Transform(object input) { string output; // Do some stuff to transform input to output... return output; } public Type GetOutputType() { return typeof(string); } } //

Run a method before and after a called method in Java

放肆的年华 提交于 2020-01-03 07:23:32
问题 I'm trying to write a Java program such that after calling a methodA() , first a method named methodBeforeA() is called and then methodA() gets executed followed by another method being called named, methodAfterA() . This is very similar to what Junit does using Annotations (using the @Before, @Test, @After), so i think it should be possible using reflection but i don't have a very good clue. 回答1: AspectJ allows you to specify cutpoints before method entry and after method exit. http://www

Strange Effect with Overridden Properties and Reflection

与世无争的帅哥 提交于 2020-01-03 07:01:52
问题 I've come across a strange behaviour in .NET/Reflection and cannot find any solution/explanation for this: class A { public virtual string TestString { get; set; } } class B : A { public override string TestString { get { return "x"; } } } Since properties are just pairs of functions ( get_PropName() , set_PropName() ) overriding only the "get" part should leave the "set" part as it is in the base class. And this is just what happens if you try to instanciate class B and assign a value to