reflection

Get a generic method without using GetMethods

I want to get the method System.Linq.Queryable.OrderyBy(the IQueryable source, Expression> keySelector) mehthod, but I keep coming up with nulls. var type = typeof(T); var propertyInfo = type.GetProperty(group.PropertyName); var propertyType = propertyInfo.PropertyType; var sorterType = typeof(Func<,>).MakeGenericType(type, propertyTyp...

iterate static int values in java

Hello. I have a simple question. Is there a way ( using reflections I suppose ) to iterate all the static values of a class? For instance class Any { static int one = 1; static int two = 2; static int three = 3; public static void main( String [] args ) { for( int i : magicMethod( Any.class ) ){ ...

Can you detect if a C# field has been assigned a default value?

Say you have a class declaration, e.g.: class MyClass { int myInt=7; int myOtherInt; } Now, is there a way in generic code, using reflection (or any other means, for that matter), that I can deduce that myInt has a default value assigned, whereas myOtherInt does not? Note the difference between being initialised with an explicit ...

Whats a fast way to check that reference is a specific generic type?

If I have a method with a parameter that's an interface, whats the fasts way to see if the interface's reference is of a specific generic type? More specifically, if I have: interface IVehicle{} class Car<T> : IVehicle {} CheckType(IVehicle param) { // How do I check that param is Car<int>? } I'm also going to have to cast afte...

How can get all attributes on a property's interface/basetype ancestry?

So, if i have: public class Sedan : Car { /// ... } public class Car : Vehicle, ITurn { [MyCustomAttribute(1)] public int TurningRadius { get; set; } } public abstract class Vehicle : ITurn { [MyCustomAttribute(2)] public int TurningRadius { get; set; } } public interface ITurn { [MyCustomAttribute(3)] in...

Get method name and type using lambda expression

I am trying to write a function that will pull the name of a property and the type using syntax like bellow private class SomeClass { Public string Col1; } PropertyMapper<Somewhere> propertyMapper = new PropertyMapper<Somewhere>(); propertyMapper.MapProperty(x => x.Col1) Is there any why to pass the property through to the functi...

How do I ignore the inheritance chain when getting attributes?

For some reason I'm not getting this. (Example model below) If I write: var property = typeof(sedan).GetProperty("TurningRadius"); Attribute.GetCustomAttributes(property,typeof(MyAttribute), false) the call will return MyAttribute(2) despite indicating I don't want to search the inheritance chain. Does anyone know what code I can wri...

Javascript Reflection

Is there a way to get all methods (private, privileged, or public) of a javascript object from within? Here's the sample object: var Test = function() { // private methods function testOne() {} function testTwo() {} function testThree() {} // public methods function getMethods() { for (i in this) { alert(i)...

How to determine the attached type from within a custom attribute?

I have a custom attribute which can be assigned to a class, [FooAttribute]. What I would like to do, from within the attribute, is determine which type has actually used me. e.g. If I have: [FooAttribute] public class Bar { } In the code for FooAttribute, how can I determine it was Bar class that added me? I'm not specifically looking...

Setting variables by name in Java

I'm looking to implement something in Java along the lines of: class Foo{ private int lorem; // private int ipsum; public setAttribute(String attr, int val){ //sets attribute based on name } public static void main(String [] args){ Foo f = new Foo(); f.setAttribute("lorem",1); f.setAttribute("ipsum",2); } public ...

call a Method with parameters got from generic Method

Hi, I have a class storing the name of a WS method to call and the type and value of the only parameter that service receives (it will be a collection of parameters but lets keep it simple for the example): public class MethodCall { public string Method { get; set; } public Type ParType { get; set; } public string ParValue { get; ...

How do I use reflection to get properties explicitly implementing an interface?

More specifically, if I have: public class TempClass : TempInterface { int TempInterface.TempProperty { get; set; } int TempInterface.TempProperty2 { get; set; } public int TempProperty { get; set; } } public interface TempInterface { int TempProperty { ...

How do I use .Net reflection to search for a property by name ignoring case?

I had the following line snippet of code that searches for a propery of an instance by name: var prop = Backend.GetType().GetProperty(fieldName); Now I want to ignore the case of fieldName, so I tried the following: var prop = Backend.GetType().GetProperty(fieldName, BindingFlags.IgnoreCase); ... No dice. Now prop won't find field...

How to work with an interface dynamically loaded from an assembly and invoke its members

I have got some code to load an assembly and get all types, which implement a certain interface, like this (assume asm is a valid and loaded assembly). var results = from type in asm.GetTypes() where typeof(IServiceJob).IsAssignableFrom(type) select type; Now I'm stuck: I need to create instances of these objects and invoke method...

What is the C# equivalent to Java's instanceof and isInstance()?

I know of is and as for instanceof, but what about the reflective isInstance() method? ...

Which class invoked my static method?

Suppose that I have a Java class with a static method, like so: class A { static void foo() { // Which class invoked me? } } And suppose further that class A has an arbitrary number of subclasses: class B extends A { } class C extends A { } class D extends A { } ... Now consider the following method invocation...

Java Reflection: How to know if a Method is static?

I want to discover at run-time ONLY the static Methods of a class, how can I do this? Or, how to differentiate between static and non-static methods. ...

How does reflection tell me when a property is hiding an inherited member with the 'new' keyword?

So if I have: public class ChildClass : BaseClass { public new virtual string TempProperty { get; set; } } public class BaseClass { public virtual string TempProperty { get; set; } } How can I use reflection to see that ChildClass is hiding the Base implementation of TempProperty? I'd like the answer to be agnostic between c...

what java.lang.reflect.Method.isBridge () used for ?

During navigation of Method class I came across the function isBridge(), javadoc of which says, that its true only if java spec declares the method as true. Please help me understand what this is used for ? Can a custom class declare its method as a bridge if required ? ...

Why Type.Equals(t1, t2) and not the equality operator?

Why must Type.Equals(t1, t2) be used to determine equivalent types, and not the equality operator (e.g. for VB.NET, t1 = t2)? It seems inconsistent with other parts of the .NET API. Example in VB.NET: If GetType(String) = GetType(String) Then Debug.Print("The same, of course") End If causes a compile-time error of "Operator '=...