reflection

How to get Column names of JPA entity

自闭症网瘾萝莉.ら 提交于 2020-01-13 02:42:20
问题 All my JPA entity classes implement an interface called Entity which is defined like this: public interface Entity extends Serializable { // some methods } Some of the fields of my JPA entity have @Column annotation on top of them and some don't. MyEntity class is defined like below: @Entity public class MyEntity implements Entity { @Id private Long id; // Assume that it is auto-generated using a sequence. @Column(name="field1") private String field1; private SecureString field2; /

Why does Type.GetElementType return null?

荒凉一梦 提交于 2020-01-13 02:39:32
问题 I have a method that takes as an input parameter an object of type IEnumerable . I want to enumerate the enumeration and for each item use reflection to get the value for each property. I have the following code: protected void WriteData(IEnumerable data) { var enumerationTypeInfo = data.GetType(); var itemTypeInfo = enumerationTypeInfo.GetElementType(); ... } The problem is enumerationTypeInfo.GetElementType() always returns null . In particular, I'm passing in a List<Entry> into WriteData ,

JUnit 4 and Suspend-on-Exception

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-12 14:29:14
问题 When an uncaught exception is thrown in my code, I am used to having the debugger stop at the throwing statement so that I can examine the local variables and the members of all objects involved at the moment that the exception was thrown. With IntelliJ Idea this can be accomplished by going to Run , View Breakpoints , selecting the Exception Breakpoints tab, checking Any exception , and making sure that the Caught exception checkbox is unchecked, while the Uncaught exception checkbox is

How do I get a list of all loaded Types in C#?

久未见 提交于 2020-01-12 14:11:31
问题 I need to retrieve all enums that were loaded from a given set of Assemblies. 回答1: List<Type> list = new List<Type>(); foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type t in ass.GetExportedTypes()) { if (t.IsEnum) { list.Add(t); } } } That should do, for all assemblies loaded by the current Appdomain, to get just from defined assemblies, just adjust ;-) 回答2: Here is a more functional solution: AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes(

How do I get a list of all loaded Types in C#?

ぃ、小莉子 提交于 2020-01-12 14:11:29
问题 I need to retrieve all enums that were loaded from a given set of Assemblies. 回答1: List<Type> list = new List<Type>(); foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type t in ass.GetExportedTypes()) { if (t.IsEnum) { list.Add(t); } } } That should do, for all assemblies loaded by the current Appdomain, to get just from defined assemblies, just adjust ;-) 回答2: Here is a more functional solution: AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes(

Reflection: How do I find and invoke a local functon in C# 7.0?

空扰寡人 提交于 2020-01-12 12:43:12
问题 I have a private static generic method I want to call using reflection, but really I want to 'bundle' it inside of another method. C# 7.0 supports local functions so this is definitely possible. You would say "why don't you just call it directly?" but I'm using it to get the ability to use an object and System.Type in a strongly typed manner so I need to call it dynamically. This code already works if I have it as it's own private static generic method. private static void HandleResponse

Reflection: How do I find and invoke a local functon in C# 7.0?

我怕爱的太早我们不能终老 提交于 2020-01-12 12:40:11
问题 I have a private static generic method I want to call using reflection, but really I want to 'bundle' it inside of another method. C# 7.0 supports local functions so this is definitely possible. You would say "why don't you just call it directly?" but I'm using it to get the ability to use an object and System.Type in a strongly typed manner so I need to call it dynamically. This code already works if I have it as it's own private static generic method. private static void HandleResponse

ICSharpCode.Decompiler + Mono.Cecil -> How to generate code for a single method?

∥☆過路亽.° 提交于 2020-01-12 09:15:16
问题 I'm able to use Mono.Cecil and ICSharpCode.Decompiler to generate the code for a type or an assembly. But if I try to generate the code for a single method I'll get an error "Object reference not set to an instance of an object." Can you guys give me any hints about this? Thanks ahead for all the help. Code to generate code for all the types inside an assembly: DirectoryInfo di = new DirectoryInfo(appPath); FileInfo[] allAssemblies = di.GetFiles("*.dll"); foreach (var assemblyFile in

Using a Delegate to call a constructor

喜你入骨 提交于 2020-01-12 08:01:47
问题 I found this but tried to use it and failed. How can i create an object using reflections and make it fast by putting it in a delegate? DynamicMethod dm = new DynamicMethod("MyCtor", t, new Type[] { }); var ctor = t.GetConstructor(new Type[] { }); ILGenerator ilgen = dm.GetILGenerator(); ilgen.Emit(OpCodes.Ldarg_0); ilgen.Emit(OpCodes.Newobj, ctor); ilgen.Emit(OpCodes.Ret); var d = (Func<T>)dm.CreateDelegate(t); dm.Invoke(null, new object[] { }); Before putting it in a deleage i tried to at

How to cast an object to a Type extracted at runtime

夙愿已清 提交于 2020-01-12 07:27:28
问题 I am using reflection to get an object's type, or for this issue an object that has instance properties type, at runtime and then I need to change an existing variable's type into that newly found type. Is this possible? For example, the following code does not work in the line indicated within: Public Sub DoSomething(x As T, y As T, exp As String) 'This is a instance property on the object of a different type 'i.e. 'T.AnotherType' We need to reflect to find out what type of object