reflection

What is the supertype of all functions in Scala?

ε祈祈猫儿з 提交于 2020-01-04 02:44:14
问题 I know I can do instanceOf checks against Function1 or Function2 etc but is there a generic way to see if something is function or not (it can have arbitray number of args). I tried defining something like this: type FuncType = (Any*) -> Any But that did not work either. Basically I have some code that looks like this: call = (name: Any, args: Any*) -> if name.isFunction then name.castAs[Function].apply(args) else name aFunction = (name: String) => "Hello " + name notAFunction = "Hello rick"

object created with “new” keyword and created with reflection

独自空忆成欢 提交于 2020-01-04 02:19:06
问题 I came to know that using Reflection we can create objects without using "new" keyword. So I wanted to know are there any differences in them or any particular scenarios to use Reflection. Because till now i didnt create or seen any of the code creating object with reflection. Why using 'new' became so used and reflection not. 回答1: You'd use reflection only when you want to instantiate a type you based on its name (a string) or a type that you don't have access at compile time (e.g. a plugin)

How to use reflection to add a new item to a collection

浪尽此生 提交于 2020-01-04 02:07:24
问题 I'm trying to use reflection to add an unknown object to an unknown collection type, and I'm getting an exception when I actually perform the "Add". I wonder if anyone can point out what I'm doing wrong or an alternative? My basic approach is to iterate through an IEnumerable which was retrieved through reflection, and then adding new items to a secondary collection I can use later as the replacement collection (containing some updated values): IEnumerable businessObjectCollection =

C# using properties with value types with Delegate.CreateDelegate

不问归期 提交于 2020-01-04 01:56:06
问题 Using Jon Skeet's article Making reflection fly and exploring delegates as a guide, I am trying to use the Delegate.CreateDelegate method to duplicate properties as delegates. Here's an example class: public class PropertyGetter { public int Prop1 {get;set;} public string Prop2 {get;set;} public object GetPropValue(string propertyName) { var property = GetType().GetProperty(propertyName).GetGetMethod(); propertyDelegate = (Func<object>)Delegate.CreateDelegate(typeof(Func<object>), this,

reordering XML tags

此生再无相见时 提交于 2020-01-04 01:50:55
问题 I am trying to implement something which is for writing back the content tree of java object to XML file (object marshalling) (I know there are a lot of APIs for doing that but its required from me), I want to let the user to reorder the tags as he/she wants, I know using annotation like what JAXB has may solve that, but I think using annotation may cause a lot of pain. it will be so helpful if any one could offer any good approach. Thanks 回答1: Note: I'm the EclipseLink JAXB (MOXy) lead and a

Most efficient way to get all types in AppDomain marked with a specific attribute?

五迷三道 提交于 2020-01-04 01:19:21
问题 If I do this, I enumerate over all types in my program: List<SerializableAttribute> attributes=new List<SerializableAttribute>() ; foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { attributes.AddRange( type.GetCustomAttributes(false) .OfType<SerializableAttribute>() .ToList()); } } Is metadata that comes with a .NET dll indexed to allow me to do something like: List<SerializableAttribute> attributes = typeof

Can I get the Type() of a bound object in C#/WPF (even if the bound value is null)?

自闭症网瘾萝莉.ら 提交于 2020-01-03 20:58:11
问题 I have a binding to an unknown source. All I have is the binding. I have no other way of looking at the bound object. I need to figure out the Type for the bound object, even if the value is null (this is where my problem is). I was evaluating the binding by binding to an object and then using the object as a way to get the Type, but I need to know the type even if the value is null. For instance, I have a class like so: public class Customer{ public string Name { get; set; } public int Age {

How can I get a reference to a Kotlin object by name?

荒凉一梦 提交于 2020-01-03 20:04:17
问题 If I have a top level object declaration package com.example object MyObject {} how can I convert the string com.example.MyObject into a reference to MyObject ? 回答1: If you have kotlin-reflect on the classpath then you can use the objectInstance property of KClass fun main(args: Array<String>) { val fqn = "com.example.MyObject" val clz: Class<*> = Class.forName(fqn) val instance = clz.kotlin.objectInstance println(instance) // com.example.MyObject@71623278 } if you don't have kotlin-reflect

How do you determine if a method returns a dynamic type when using reflection?

本小妞迷上赌 提交于 2020-01-03 19:33:10
问题 When using reflection, fields, properties, indexers, and parameters can be examined for the DynamicAttribute attribute in order to determine that they have a dynamic type. However, this doesn't work for Methods - even though they return 'dynamic', they have NO attributes and their return type is 'object' (and also has no attributes). Visual Studio does this for intellisense for methods in external assemblies... can it be done with reflection? For example, the code below produces this output:

Get the class function name within itself

梦想的初衷 提交于 2020-01-03 19:08:31
问题 I'm trying to setup logging across my typescript program, using log4javascript . However I have no idea how to retrieve the function names using reflection (rather than typed manually). Ideally I want to emulate what I do in C# : public class Foo { private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Foo)); public Foo() { } public FooMethod() { try { logger.Logger.Log(this.GetType(), log4net.Core.Level.Trace, "Entering" + MethodBase.GetCurrentMethod().Name, null);