Using Reflection, can we find the methods effected in a changelist(check-in), we have line numbers that has the changes in a source file. Using C# reflection we can get methods in module/class. I am not sure how to find methods that belong for a given line number. The plan is to run all the tests that call these methods, that have changes. Any ideas friends? Thanks!
jeudi 1 octobre 2015
Extract type hint from ReflectionParameter
class C
{
function methodA(Exception $a, array $id){}
}
function getClassName(ReflectionParameter $param) {
$regex = '/\[([^\]]*)\]/';
preg_match($regex, $param->__toString(), $matches);
return isset($matches[1]) ? $matches[1] : null;
}
foreach( new ReflectionMethod('C', 'methodA')->getParameters() as $param)
{
echo getClassName($param);
}
Want returned value to simply be 'Exception' and 'array' not "Exception $a" and "array $id". What should the regex be. Working on php 5.3
Use Reflection to Get a Method which Takes Action
Specifically, I am looking at a bunch of extension methods which are all overloaded like this (they're part of SignalR, if you couldn't guess):
public static IDisposable On(this IHubProxy proxy, string eventName, Action onData);
public static IDisposable On<T>(this IHubProxy proxy, string eventName, Action<T> onData);
public static IDisposable On<T1, T2>(this IHubProxy proxy, string eventName, Action<T1, T2> onData);
Now I have no problem getting the methodInfo for the first (non-generic) On method by doing this:
var methodInfo = typeof(HubProxyExtensions).GetMethod("On", new[] {typeof(IHubProxy), typeof(string), typeof(Action)});
However, I want to be able to get the second or third definition of the "On" method. However, I've found that something like this does not work:
var methodInfo = typeof(HubProxyExtensions).GetMethod("On", new[] {typeof(IHubProxy), typeof(string), typeof(Action<>)});
In the above case, methodInfo ends up being null. Any ideas?
How to get slice underlying value via reflect.Value
I read the reflect document and I'm a little confused about why it doesn't have a func (v Value) Slice() slice
function, which to get the underlying value from a reflect.Value which holds a slice in.
Is there a convenient way to get the underlying slice from a reflect.Value ?
Dynamically created Proxy a class
I'm building a class at runtime and creating objects of that.
After that, I've several objects of this generated class.
So, I'd like to proxy these objects:
IInterceptor[] interceptors = new IInterceptor[1];
interceptors[0] = new Interceptors.IEditableObjectInterceptor<object>();
return DynamicExtensions.proxyGenerator.CreateClassProxyWithTarget(baseType, target, options, interceptors);
When I perform CreateClassProxyWithTarget
it chrashes dumping me:
Can not instantiate proxy of class: DynamicDigitalInput.
Could not find a parameterless constructor.
So, the message is clear. However, I've tried the next:
System.Reflection.ConstructorInfo cInfo = baseType.GetConstructor(new Type[] { });
Assert.That(cInfo != null);
var constructor = baseType.GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic,null, Type.EmptyTypes, null);
Assert.That(constructor != null);
object d = Activator.CreateInstance(baseType, new object[] {});
Assert.That(d != null);
And it's work well. So I can get default constructors and instantiate a DynamicDigitalInput
class object.
Where's the problem?
Performance of frameworks with reflection
In most frameworks (Struts, Spring, Hibernate...) we are specifying the full package names of our class in XML, or annotation. Framework use this information for looking that class, Instantiating it, looking a method it it, and invoking that method.
All these operations use reflection, and this lookup, instantiation, and invoking is a very frequent in a web application. If reflection is a heavy process (needs more CPU, more memory, and more time consuming), Is it worth to use these frameworks for time critical applications?
Is there any optimization techniques that makes Reflection as fast as normal instantiation, and invoking?
Auslesen der Elemente einer verschachtelten Klasse c#
ich muss alle Elemente einer Klasse auslesen und in einem Tree darstellen.
Das Tree-Handling kann aussen vor bleiben, mir genügt als Lösung eine Liste.
Alle Beispiele die ich finden konnte (z.B. Recursively Get Properties & Child Properties Of A Class) geben bei folgender Klasse "cTest" nur die beiden ersten Elemente aus.
public class cTest
{
public String str1;
public int intwert1;
public cParent Parent = new cParent();
}
public class cParent
{
public String parentStr1;
}
Das Element "public cParent Parent" wird nicht gefunden und nicht rekursiv abgearbeitet. Ich konnte schon feststellen, das bei
Type t = typeof(cTest);
PropertyInfo[] propertyInfos;
MemberInfo[] propertyMembers;
propertyInfos = t.GetProperties();
propertyMembers = t.GetMembers();
die propertyInfos das "public cParent Parent" nicht enthalten
die propertyMembers das "public cParent Parent" beinhalten!
ANY HELP WELCOME ;)