reflection

Cloning/Copying get accessor body to new type

十年热恋 提交于 2020-01-14 14:13:29
问题 I'm creating new type in dynamic assembly from existing type, but with only selected properties to include: public class EmitTest { public Type Create(Type prototype, Type dynamicBaseType, List<string> includedPropertyList) { AssemblyName aName = new AssemblyName("DynamicAssembly"); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( aName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder modulBuilder = assemblyBuilder.DefineDynamicModule(aName.Name, aName.Name +

Call all methods within a class

我是研究僧i 提交于 2020-01-14 14:10:03
问题 I have a class, which has a method calls all of the rest methods within the same class. One way to do it is by using reflection framework, are there other ways? [edit] Example code added: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class AClass { private void aMethod(){ } private void bMethod(){ } private void cMethod(){ } private void dMethod(){ } //50 more methods. //method call the rest public void callAll() throws IllegalArgumentException,

Generically rewriting Scala case classes

ぐ巨炮叔叔 提交于 2020-01-14 13:36:30
问题 Is it possible to generically replace arguments in a case class? More specifically, say I wanted a substitute function that received a "find" case class and a "replace" case class (like the left and right sides of a grammar rule) as well as a target case class, and the function would return a new case class with arguments of the find case class replaced with the replace case class? The function could also simply take a case class (Product?) and a function to be applied to all arguments

Get methodinfo for Enumerable.DefaultIfEmpty

て烟熏妆下的殇ゞ 提交于 2020-01-14 10:33:12
问题 I'm building some Linq Expression and trying to get hold of MethodInfo for IEnumerable.DefaultIfEmpty (http://msdn.microsoft.com/en-us/library/bb360179.aspx). What seemed to be an easy task but I'm clueless to why it's not working. typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>) }); typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>).MakeGenericType(typeof(WorkitemListModel)) }); 回答1: Getting generic methods is a pain, to be honest. I don

initializing properties with private sets in .Net

余生颓废 提交于 2020-01-14 09:56:11
问题 public class Foo { public string Name { get; private set;} // <-- Because set is private, } void Main() { var bar = new Foo {Name = "baz"}; // <-- This doesn't compile /*The property or indexer 'UserQuery.Foo.Name' cannot be used in this context because the set accessor is inaccessible*/ using (DataContext dc = new DataContext(Connection)) { // yet the following line works. **How**? IEnumerable<Foo> qux = dc.ExecuteQuery<Foo>( "SELECT Name FROM Customer"); } foreach (q in qux) Console

initializing properties with private sets in .Net

时光毁灭记忆、已成空白 提交于 2020-01-14 09:56:06
问题 public class Foo { public string Name { get; private set;} // <-- Because set is private, } void Main() { var bar = new Foo {Name = "baz"}; // <-- This doesn't compile /*The property or indexer 'UserQuery.Foo.Name' cannot be used in this context because the set accessor is inaccessible*/ using (DataContext dc = new DataContext(Connection)) { // yet the following line works. **How**? IEnumerable<Foo> qux = dc.ExecuteQuery<Foo>( "SELECT Name FROM Customer"); } foreach (q in qux) Console

How to get the property that has a DataMemberAttribute with a specified name?

喜你入骨 提交于 2020-01-14 08:03:12
问题 How can I reflectively get the property that has the DataMember with a given name (let's assume every DataMember has a unique name)? For example, in the following code the property with the DataMember that has name "p1" is PropertyOne : [DataContract(Name = "MyContract")] public class MyContract { [DataMember(Name = "p1")] public string PropertyOne { get; set; } [DataMember(Name = "p2")] public string PropertyTwo { get; set; } [DataMember(Name = "p3")] public string PropertyThree { get; set;

How to find MethodInfo for a method of a generic class using strongly-typed reflection?

喜夏-厌秋 提交于 2020-01-14 07:33:13
问题 I'd like to get a MethodInfo of a method from a generic class having a type parameter known only at runtime. Here is how I would get a MethodInfo for a generic method from a non-generic class: class MyClass { public void MyMethod<T> (T arg) { } } static MethodInfo Resolve (Type type) { Expression<Action<MyClass, object>> lambda = (c, a) => c.MyMethod (a); MethodCallExpression call = lambda.Body as MethodCallExpression; return call .Method // Get MethodInfo for MyClass.MyMethod<object>

Create Expression<Func<T, object>> from a given Type

怎甘沉沦 提交于 2020-01-14 07:00:15
问题 I am looking to use CsvHelper dynamically by building up Expressions in code which represent property member access for a given type. The method I am trying to pass these expressions to has the following signature: public virtual CsvPropertyMap<TClass, TProperty> Map<TProperty>( Expression<Func<TClass, TProperty>> expression ) { // } So you would normally call it, for any given type you want to map, like this (for a type with a property called 'stringProperty'): mapper.Map(x => x

In scala 2.12, why none of the TypeTag created in runtime is serializable?

独自空忆成欢 提交于 2020-01-14 05:46:13
问题 The bounty expires in 2 days . Answers to this question are eligible for a +100 reputation bounty. tribbloid wants to draw more attention to this question. I'm seeking a method to create a serializable TypeTag without using compile time tools (completely relying on runtime). This is a basic feature for all reflective language. The answer in this post proposed a few methods: In Scala, how to create a TypeTag from a type that is serializable? NONE OF THEM WORKED: package com.tribbloids.spike