reflection

Use Reflection to call generic method on object instance with signature: SomeObject.SomeGenericInstanceMethod<T>(T argument)

元气小坏坏 提交于 2019-12-28 07:03:11
问题 How do I call SomeObject.SomeGenericInstanceMethod<T>(T arg) ? There are a few posts about calling generic methods, but not quite like this one. The problem is that the method argument parameter is constrained to the generic parameter. I know that if the signature were instead SomeObject.SomeGenericInstanceMethod<T>(string arg) then I could get the MethodInfo with typeof (SomeObject).GetMethod("SomeGenericInstanceMethod", new Type[]{typeof (string)}).MakeGenericMethod(typeof(GenericParameter)

Golang reflect: Get Type representation from name?

被刻印的时光 ゝ 提交于 2019-12-28 07:00:49
问题 Is there a way to use the reflection libraries in Go to go from the name of a type to its Type representation? I've got a library where the user needs to provide Type representations for some code generation. I know it must be possible (in a sense) because they can just create a variable of that type and call the TypeOf function, but is there a way to circumvent this and just get representation from the name? 回答1: The question is not quite explicit, it can be interpreted in 2 ways, to one of

Android, how to clear the recent task list which could get from Home button in most phone? Reflection is a possible way?

99封情书 提交于 2019-12-28 06:24:10
问题 I'm writing a Launcher, it need clear the recent app/task list from system, not "didn't show my apps in recent tasks list", but I have no idea about it now. I have searched in the stackoverflow and only this issue is matched but the answer has no any help. Some other guy has asked the same questions, he metioned the RemoveTask which comes from Android 4.0. Yes, I have checked the source code of Android 2.3.7 and Android 4.0, at a round estimated, I think I could almost reach the end point if

Get member to which attribute was applied from inside attribute constructor?

懵懂的女人 提交于 2019-12-28 05:58:50
问题 I have a custom attribute, inside the constructor of my custom attribute I want to set the value of a property of my attribute to the type of the property my attribute was applied to, is there someway to access the member that the attribute was applied to from inside my attribute class? 回答1: Attributes don't work that way, I'm afraid. They are merely "markers", attached to objects, but unable to interact with them. Attributes themselves should usually be devoid of behaviour, simply containing

Get user-friendly name for generic type in C#

為{幸葍}努か 提交于 2019-12-28 05:58:11
问题 Is there an easy way without writing a recursive method which will give a 'user friendly' name for a generic type from the Type class? E.g. For the following code I want something like 'List<Dictionary<Int>>' instead of the shorthand or full name given by the following code: var list = new List<Dictionary<int, string>>(); var type = list.GetType(); Console.WriteLine(type.Name); Console.WriteLine(type.FullName); 回答1: Based on your edited question, you want something like this: public static

Reflection Performance - Create Delegate (Properties C#)

感情迁移 提交于 2019-12-28 05:01:04
问题 I'm having performance problems with using reflection. So I decided to create delegates for the properties of my objects and so far got this: TestClass cwp = new TestClass(); var propertyInt = typeof(TestClass).GetProperties().Single(obj => obj.Name == "AnyValue"); var access = BuildGetAccessor(propertyInt.GetGetMethod()); var result = access(cwp); static Func<object, object> BuildGetAccessor(MethodInfo method) { var obj = Expression.Parameter(typeof(object), "o"); Expression<Func<object,

Create custom AppDomain and add assemblies to it

大憨熊 提交于 2019-12-28 04:15:11
问题 How can I create an appdomain, add assemblies to it, then destroy that app domain? This is what I have tried: static void Main(string[] args) { string pathToExe = @"A:\Users\Tono\Desktop\ConsoleApplication1.exe"; AppDomain myDomain = AppDomain.CreateDomain("MyDomain"); Assembly a = Assembly.Load(System.IO.File.ReadAllBytes(pathToExe)); myDomain.Load(a.FullName); // Crashes here! } I have also tried: myDomain.Load(File.ReadAllBytes(pathToExe)); how can I add an assembly to the appdomain. Once

How to call a private method from outside a java class

倖福魔咒の 提交于 2019-12-28 02:37:04
问题 I have a Dummy class that has a private method called sayHello . I want to call sayHello from outside Dummy . I think it should be possible with reflection but I get an IllegalAccessException . Any ideas??? 回答1: use setAccessible(true) on your Method object before using its invoke method. import java.lang.reflect.*; class Dummy{ private void foo(){ System.out.println("hello foo()"); } } class Test{ public static void main(String[] args) throws Exception { Dummy d = new Dummy(); Method m =

Instantiating a constructor with parameters in an internal class with reflection

依然范特西╮ 提交于 2019-12-27 23:37:58
问题 I have something along the lines of this: object[] parameter = new object[1]; parameter[0] = x; object instantiatedType = Activator.CreateInstance(typeToInstantiate, parameter); and internal class xxx : ICompare<Type> { private object[] x; # region Constructors internal xxx(object[] x) { this.x = x; } internal xxx() { } ... } And I get: threw exception: System.MissingMethodException: Constructor on type 'xxxx.xxx' not found.. Any ideas? 回答1: The issue is that Activator.CreateInstance(Type,

C# instantiate generic List from reflected Type [duplicate]

最后都变了- 提交于 2019-12-27 23:37:12
问题 This question already has answers here : Pass An Instantiated System.Type as a Type Parameter for a Generic Class (6 answers) Closed 5 years ago . Is it possible to create a generic object from a reflected type in C# (.Net 2.0)? void foobar(Type t){ IList<t> newList = new List<t>(); //this doesn't work //... } The Type, t, is not known until runtime. 回答1: Try this: void foobar(Type t) { var listType = typeof(List<>); var constructedListType = listType.MakeGenericType(t); var instance =