reflection

How to map datatable columns to properties of DTO object of Type T

风格不统一 提交于 2020-01-02 20:30:17
问题 How can map datatable columns with the properties of DTO object of Type T using LINQ query or Lambda expression. This should automatically map with any DTO. If there is a way to do this without hardcoding of column names of datatable DataTable dt = db.GetEmployees(); foreach(DataRow dr in dt.Rows) { var obj = new T(); PropertyInfo[] prop = obj.GetType().GetProperties(); var results = dt.AsEnumerable().Select(dr => new T { ///How to directly map type T properties in prop with columns in

How do you get the type of generic argument from the stackframe?

萝らか妹 提交于 2020-01-02 19:22:08
问题 We are supposed to instantiate our entities through a factory since they are set up differently on the client and server. I want to make sure this is the case but cant quite get it to work. public interface IEntityFactory { TEntity Create<TEntity>() where TEntity : new(); } public abstract class Entity { protected Entity() { VerifyEntityIsCreatedThroughFactory(); } [Conditional("DEBUG")] private void VerifyEntityIsCreatedThroughFactory() { foreach (var methodBase in new StackTrace().GetFrames

Getting the array Class<?> of a given class

眉间皱痕 提交于 2020-01-02 18:23:08
问题 Given a Class<?> that describes class A , is it somehow possible to get the Class<?> that matches the class A[] ? Class<?> clazz = A.class; Class<?> arrayclazz = clazz.toArray(); // ?? assert arrayclazz.equals(A[].class); 回答1: java.lang.reflect.Array.newInstance(clazz, 0).getClass() 回答2: use A[].class . I hope this help. 来源: https://stackoverflow.com/questions/5091348/getting-the-array-class-of-a-given-class

How can I get all (non-final) object vals and subobject vals using reflection in Scala?

不羁岁月 提交于 2020-01-02 14:13:36
问题 Note: This question is not a duplicate of How can I get all object vals and subobject vals using reflection in Scala? The answer provided in that question only works for final members. For example: scala> object Settings { | val Host = "host" | } defined module Settings deepMembers(Settings) res0: Map[String,String] = Map() 回答1: It must be a duplicate, but I need a refresher: $ scala Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45). Type in expressions to

How can I get all (non-final) object vals and subobject vals using reflection in Scala?

旧时模样 提交于 2020-01-02 14:11:02
问题 Note: This question is not a duplicate of How can I get all object vals and subobject vals using reflection in Scala? The answer provided in that question only works for final members. For example: scala> object Settings { | val Host = "host" | } defined module Settings deepMembers(Settings) res0: Map[String,String] = Map() 回答1: It must be a duplicate, but I need a refresher: $ scala Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45). Type in expressions to

Java reflection performance - alternatives

[亡魂溺海] 提交于 2020-01-02 10:29:20
问题 Topic discussed in various questions (Reference-1, Reference-2). But why do I see a performance much worse for lambda reflection? Often it is claimed that lambda reflection is as fast as direct access. But the test below shows other results. Here field reflections seems to be almost as fast as lambda reflection. To repeat test: source code follows further below. Output of test: run: > Test: Main (Class=Person1, name=Age) > RUN 0 Loops: 50000000 Warmup createLambda: new getter for clazz=lambda

Upto What extent Reflection should be used?

落爺英雄遲暮 提交于 2020-01-02 10:04:19
问题 We've got into a very tricky scenario in a project. We have used lot of reflection in our project. We have .. Validation Framework driven by Attributes and Reflection Extension methods that transaforms a DataRow to an Entity Object using Attributes and Reflection and vice versa. The same thing we have done for DataTable and EntityCollections. IComparer interface implemented on generic EntityComparer class that uses reflection to compare two Entity OBjects. Like above scenarios we have used

Java 9 dependency issues regarding serviceloader

泪湿孤枕 提交于 2020-01-02 09:10:51
问题 I have a question about how the serviceloader changed in Java 9 based on this scenario Scenario Project gert Class Main package gert; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class Main { static public void test() throws JAXBException { InputStream is = new ByteArrayInputStream("<Classes RUNTIME_INCLUDE_JARS=\"\">

Error when use callBy on a function with default parameters in Kotlin

我与影子孤独终老i 提交于 2020-01-02 08:54:38
问题 I try to call a function with default parameters values without put parameters in Kotlin. For example: class Test { fun callMeWithoutParams(value : Double = 0.5) = value * 0.5 fun callIt(name: String) = this.javaClass.kotlin .members.first { it.name == name } .callBy(emptyMap()) } fun main(args: Array<String>) { println(Test().callIt("callMeWithoutParams")) } I have the exception: Exception in thread "main" java.lang.IllegalArgumentException: No argument provided for a required parameter:

Hiding my security key from java reflection

风格不统一 提交于 2020-01-02 08:38:14
问题 The below class is my security key provider for encryption. public class MySecretKey { private String key="2sfdsdf7787fgrtdfg#$%@cj5"; ... //Some Util methods goes on Here . } First I didn't belive that one can access the private data without a getter, but, My goodness, I am able to see the key in the console now . And somewhere else using reflection we can see the code like below : public static void main(String[] args) throws Exception { Stacker myClass = new Stacker(); Field key= myClass