reflection

How to deep copy array items with unknown type at runtime without using MakeGenericMethod?

喜欢而已 提交于 2019-12-25 00:40:20
问题 I got an System.Object (AKA object) that is simple one-dimensional array (I do a lot of checks). The element type will become known at runtime. I have to do two types of copying the array: 1) deep copy array itself but shallow copy items: - this has answer here: my previous question 2) deep copy array itself as well as its items: I did it like that: public object FullDeepCopy_SimpleArray(object original) { Type elementType = original.GetType().GetElementType(); return GetType() .GetMethod(

How to reduce the usage of IF - ELSE by reflection ? Can I get the code example

情到浓时终转凉″ 提交于 2019-12-24 21:02:53
问题 I was trying to use reflection for the code of PizzaFactory Class so that I can remove the if else condition and make my code more dynamic. But I am not able to figure out how. Pizza.java package PizzaTrail; import java.util.List; //this is the main abstract factory which will be extended by the concrete factory public abstract class Pizza { public abstract List fetchIngredients(String Type); } PizzaFactory.java package PizzaTrail; import java.util.List; //this is the concrete factory public

Construct a System.Type for a generic interface with known type of T [duplicate]

做~自己de王妃 提交于 2019-12-24 20:56:40
问题 This question already has answers here : C# use System.Type as Generic parameter (2 answers) Closed last year . PROBLEM DESCRIPTION I have an interface definition IFoo<TBar> : IFoo and a method CreateFooUsingBarType(Type barType) . I need the method to resolve an IFoo<TBar> using a dependency injection tool given a specified System.Type instance that defines TBar . Don't ask how I ended up here. I am stuck within these boundaries. EXAMPLE public IFoo CreateFooUsingBarType(Type barType) { var

Return a class, not an instance, of a concrete class that extends an abstract base class?

拈花ヽ惹草 提交于 2019-12-24 20:34:16
问题 If I have a class hierarchy like this AbstractSuperClass ConcreteClassA ConcreteClassB is it possible to have a static method in AbstractSuperClass which returns the class - not an instance - of one of the two concrete classes? I've tried returning Class<AbstractSuperClass> , but the IDE (Android Studio) says Incompatible types. Required: Class <com.example.AbstractSuperClass> Found: Class <com.example.ConcreteClassA> Here's an example of what I'm thinking, but which isn't working: public

How to get type from different namespace than System in c#?

不打扰是莪最后的温柔 提交于 2019-12-24 20:15:11
问题 I would like to create types when being given only strings of their names. Here it's obvious: Type t = System.Type.GetType("System.Double"); But when I try to get type from another namespace, like System.Drawing , the above method won't return the correct type. Working solution I've found: Assembly foundAssembly = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(assembly => assembly.GetName().Name == "System.Drawing"); Type t = foundAssembly.GetType("System.Drawing.Color"); However, it

How to get control properties then save them to XML and load it back?

独自空忆成欢 提交于 2019-12-24 19:54:05
问题 Actually I would need 4 methodes. I'm using c# .NET 3.5 and windows forms. Get ALL control properties (also sub properites like MenuItems etc.) from current form where name matches list name //don't works ok Save results to XML //don't know how to save the results Load result from XML and finally set loaded control properties from XML. //work great Now I'm doing this form step 1: public static Dictionary<string, object> xmlDictionary; public Control FindControlRecursive(Control container,

Scala Reflection: instantiating a singleton object

▼魔方 西西 提交于 2019-12-24 18:25:06
问题 I'm using the following code to instantiate a scala object. This works, but there seems to be one problem: the println is printed out twice , each time with another hashcode. import scala.reflect.runtime.universe._ import scala.reflect.runtime.{universe => ru} object Test2 { println("init"+hashCode())} val mirror = ru.runtimeMirror(getClass.getClassLoader) val m = ru.typeOf[Test2.type].members.filter(_.isConstructor).head.asMethod val m2 = mirror.reflectClass(typeOf[Test2.type].typeSymbol

Scala Reflection : why getMethods can return the val members?

那年仲夏 提交于 2019-12-24 18:08:02
问题 In Scala, I have an Abstract Class : abstract class AbstractSQLParser { def apply(input: String): Int = { println(reserveWords) return 1 } protected case class Keyword(str: String) // use Reflection here protected lazy val reserveWords: Int = this.getClass .getMethods .filter(_.getReturnType == classOf[Keyword]) .length } and another extends it : class SqlParserDemo extends AbstractSQLParser{ def apply(input: String, onError: Boolean) : Int = { apply(input) } protected val CREATE = Keyword(

Best way to implement generic setting class - Get/Set Properties reflected?

痞子三分冷 提交于 2019-12-24 17:51:48
问题 I don't know how I can make a generic settings class and hope that you can help me. First of all I want a single settings file solution. For this I have created a Singleton like this: public sealed class Settings { private static readonly Lazy<Settings> _instance = new Lazy<Settings>(() => new Settings()); private Dictionary<string, object> m_lProperties = new Dictionary<string, object>(); public void Load(string fileName) { throw new NotImplementedException(); } public void Save(string

Implementing an Interface on a dynamic type with events

醉酒当歌 提交于 2019-12-24 17:06:57
问题 I am taking in an interface, looping through the .GetEvents() return array and attempting to implement the event on my dynamic type. At the point when I try to call TypeBuilder.CreateType(), I am greeted with this lovely error: "Application method on type from assembly is overriding a method that has been overridden." If I comment out the typeBuilder.DefineMethodOverride calls that attempt to implement the interface methods, at the poin when I attempt to subscribe to the event I get the error