reflection

How do I find out if a particular delegate has already been assigned to an event?

与世无争的帅哥 提交于 2019-12-30 17:21:42
问题 I have a command button on a winform. So, if I have something like: myButton.Click += MyHandler1; myButton.Click += MyHandler2; myButton.Click += MyHandler3; How can I tell if any particular MyHandler has already been added to the Click event so it doesn't get added again somewhere else in my code? I've read how you can use GetInvocationList() for your own event's information. But I get errors when trying to get the items for my command button using various combinations. It says, "The event

Generic as method return type

孤人 提交于 2019-12-30 14:09:53
问题 I have looked around on StackOverflow to find the answer for the problem I am facing. I came across many good answers but still it doesn't answer my question. Get type of a generic parameter in Java with reflection How to find the parameterized type of the return type through inspection? Java generics: get class of generic method's return type http://qussay.com/2013/09/28/handling-java-generic-types-with-reflection/ http://gafter.blogspot.com/search?q=super+type+token So here is what I want

Can a Thread be executed as another user? (.NET 2.0/3.5)

↘锁芯ラ 提交于 2019-12-30 11:10:06
问题 I have a C# application the performs some runtime compilation of source files containing calculations into dynamic assemblies. Obviously this presents a serious security issue. From the following 'formula', the code below would be generated, and a dynamic assembly created: Formula: Int32 _index = value.LastIndexOf('.'); String _retVal = value.Substring(_index + 1); return _retVal; Code Generated: using System; namespace Dynamics { public class Evaluator { public Object Evaluate(String value)

getting the caller of a method in c#

痴心易碎 提交于 2019-12-30 10:48:08
问题 is there a way to get the instance of class that called some method? 回答1: You can get the name of the method that calls by examining the call stack. Getting the class instance is a different story, and is not easily achieved (if even possible; I never really tried to do it). You should for instance consider the possibility that your method is called from a static method, in which case there is no class instance to find. Either way, this is usually not a good thing to do. If the method needs

Find out a method's name in Groovy

天大地大妈咪最大 提交于 2019-12-30 10:04:57
问题 Is there a way in Groovy to find out the name of the called method? def myMethod() { println "This method is called method " + methodName } This, in combination with duck typing would allow for quite concise (and probably hard to read) code. 回答1: No, as with Java there's no native way of doing this. You could write an AST transform so that you could annotate the method and this could set a local variable inside the method. Or you can do it the good old Java way of generating a stackTrace, and

Can I access ItemsHost of ItemsControl using reflection?

删除回忆录丶 提交于 2019-12-30 09:58:32
问题 I'm creating custom ItemsControl that is derived from DataGrid . I need to access ItemsHost that is the Panel that actually holds rows of DataGrid . I have seen som ugly tricks to do that but I consider them worse then using reflection. So can I access ItemsHost using reflection ? And how ? 回答1: Yes I can. It is simple - I've just created property in class inheriting from DataGrid : protected Panel ItemsHost { get { return (Panel) typeof (MultiSelector).InvokeMember("ItemsHost", BindingFlags

Extracting the current value of an instance variable while walking an Expression

瘦欲@ 提交于 2019-12-30 09:41:07
问题 I am currently trying to write some code which turns C# Expressions into text. To do this, I need to not only walk through the Expression tree, but also evaluate just a little part of it - in order to get the current value of a local variable. I am finding very hard to put into words, so here is the pseudo-code instead. The missing part is in the first method: public class Program { private static void DumpExpression(Expression expression) { // how do I dump out here some text like: // set T2

Check type of member/property

爷,独闯天下 提交于 2019-12-30 09:39:33
问题 Let's say i have any class, like this one: class SomeClass(val aThing: String, val otherThing: Double) Then I use reflection to analyze the fields of this class: for(field in SomeClass.declaredMemberProperties){ } How can I check the type of each field? 回答1: Since Kotlin does not have fields but only properties with backing fields, you should check the return type of the property. Try this: class SomeClass(val aThing: String, val otherThing: Double) for(property in SomeClass::class

Best way to get a Type object from a string in .NET

馋奶兔 提交于 2019-12-30 08:56:18
问题 What is the best way to convert a string into a Type object in .NET? Issues to consider: The type may be in a different assembly. The type's assembly may not be loaded yet. This is my attempt, but it doesn't address the second issue Public Function FindType(ByVal name As String) As Type Dim base As Type base = Reflection.Assembly.GetEntryAssembly.GetType(name, False, True) If base IsNot Nothing Then Return base base = Reflection.Assembly.GetExecutingAssembly.GetType(name, False, True) If base

Get enum value from enum type and ordinal

可紊 提交于 2019-12-30 08:39:05
问题 public <E extends Enum> E decode(java.lang.reflect.Field field, int ordinal) { // TODO } Assuming field.getType().isEnum() is true , how would I produce the enum value for the given ordinal? 回答1: field.getType().getEnumConstants()[ordinal] suffices. One line; straightforward enough. 回答2: To get what you want you need to invoke YourEnum.values()[ordinal] . You can do it with reflection like this: public static <E extends Enum<E>> E decode(Field field, int ordinal) { try { Class<?> myEnum =