reflection

java reflection field type before upcasting

£可爱£侵袭症+ 提交于 2020-01-11 10:43:20
问题 I have a simple class public class SomeService { private Number number = new Integer(0); } Is it possible to find out by means of java reflection the field type before upcasting? I can just obtain Number type instead of Integer: Field field = MealService.class.getDeclaredField("number"); field.setAccessible(true); System.out.println("impl:"+field.getType()); Please advise. 回答1: field.getType() returns the declared type. If you want to know the instantiation type you have to first instantiate

ILMerge alternative, how to embed application’s dependent DLLs inside an EXE file?

橙三吉。 提交于 2020-01-11 10:29:23
问题 As stated here im trying to embed the dlls in the exe application in order to just distribute one exe, but when i try to run my application on a xp machine with full .NET 4 installed it just crashes with no error, im placing the following code on the main method [STAThread] static void Main() { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll"; using (var stream = Assembly

ILMerge alternative, how to embed application’s dependent DLLs inside an EXE file?

只谈情不闲聊 提交于 2020-01-11 10:28:28
问题 As stated here im trying to embed the dlls in the exe application in order to just distribute one exe, but when i try to run my application on a xp machine with full .NET 4 installed it just crashes with no error, im placing the following code on the main method [STAThread] static void Main() { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll"; using (var stream = Assembly

What's the best way to instantiate a generic from its name?

落爺英雄遲暮 提交于 2020-01-11 10:11:15
问题 Assuming I have only the class name of a generic as a string in the form of "MyCustomGenericCollection(of MyCustomObjectClass)" and don't know the assembly it comes from, what is the easiest way to create an instance of that object? If it helps, I know that the class implements IMyCustomInterface and is from an assembly loaded into the current AppDomain. Markus Olsson gave an excellent example here, but I don't see how to apply it to generics. 回答1: Once you parse it up, use Type.GetType

What's the best way to instantiate a generic from its name?

不想你离开。 提交于 2020-01-11 10:11:07
问题 Assuming I have only the class name of a generic as a string in the form of "MyCustomGenericCollection(of MyCustomObjectClass)" and don't know the assembly it comes from, what is the easiest way to create an instance of that object? If it helps, I know that the class implements IMyCustomInterface and is from an assembly loaded into the current AppDomain. Markus Olsson gave an excellent example here, but I don't see how to apply it to generics. 回答1: Once you parse it up, use Type.GetType

C# implicit cast “overloading” and reflection problem

瘦欲@ 提交于 2020-01-11 09:51:17
问题 I've got a problem with the following code (which compiles but crashes): using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication1 { public struct MyBoolean { public bool Value { get; set; } //cast string -> MyBoolean public static implicit operator MyBoolean(System.String value) { return new MyBoolean() { Value = (value[0] == 'J') }; } //cast bool -> MyBoolean public static implicit operator MyBoolean(bool

Get type from generic type fullname

谁说胖子不能爱 提交于 2020-01-11 09:25:55
问题 I would like to get type from generic type fullname like that : var myType = Type.GetType("MyProject.MyGenericType`1[[MyProject.MySimpleType, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"); But it seems to not work with generics types like that... What is the good method to do that ? 回答1: If you don't specify the assembly qualified name, Type.GetType works only for mscorlib types. In your example you has defined the AQN only for the embedded type argument. // this

Use dynamic R strings in Android

旧巷老猫 提交于 2020-01-11 09:21:06
问题 I'm having a problem using strings stored in my strings.xml , I have a wide list of strings stored there. They are very useful for me because I'm using them to translate my program. However, now I want to choose between those strings dynamically and I don't know how to do it. It will be easier to understand with an example. Let's assume that I have the following strings: <string name="red">Red</string> <string name="blue">Blue</string> <string name="green">Green</string> <string name="yellow"

Using GetCurrentMethod in (supposedly) high-performance code

天涯浪子 提交于 2020-01-11 08:38:12
问题 For logging purposes, some methods in our application include the following line: Dim Log As ILog = GetLog(Reflection.MethodBase.GetCurrentMethod().DeclaringType) I have what might be described as an irrational fear of reflection, which I try to keep in check. However, calls like this in methods that are executed potentially a hundred times a second concern me. I don't know as much as I should about reflection; but from looking briefly over the documentation, it looks to me like I could

Dump Object for ComObject using dynamic?

喜夏-厌秋 提交于 2020-01-11 07:11:31
问题 I'm trying (without luck) to implement an "Object Dumper" for objects I'm accessing in the Office Type Library. It must be possibly, because VS's debug window has a "dynamic view" for the System.__ComObject objects that effectively does what I want. Any ideas? 回答1: I have also created a method for getting an interface that can be used for accessing the object. Use: using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; You have to have an IDispatch interface in