reflection

The representation of an empty argument in a “call”

懵懂的女人 提交于 2020-08-17 11:55:32
问题 What kind of animal is an empty argument? Consider the following piece of code. > f <- function(...) match.call() > l <- as.list(f(,3)) > l [[1]] f [[2]] [[3]] [1] 3 > typeof(l[[2]]) [1] "symbol" > identical(l[[2]],``) Error: attempt to use zero-length variable name > as.character(l[[2]]) [1] "" What sorcery is that? 回答1: If looks like l[[2]] 's value is a special object: the empty symbol. Some facts about the empty symbol (speculated based on casual experimentation). (1) The empty symbol is

Checking compatibility of two python functions (or methods)

余生长醉 提交于 2020-08-08 04:08:46
问题 Is there a possibility to check if two python functions are interchangeable? For instance, if I have def foo(a, b): pass def bar(x, y): pass def baz(x,y,z): pass I would like a function is_compatible(a,b) that returns True when passed foo and bar, but False when passed bar and baz, so I can check if they're interchangeable before actually calling either of them. 回答1: Take a look at inspect.getargspec(): inspect.getargspec(func) Get the names and default values of a function’s arguments. A

Mono.Cecil - simple example how to get method body

爱⌒轻易说出口 提交于 2020-08-05 06:36:29
问题 I have been searching for a newbie question, but can't find a simple example. Can anyone give me a simple example how to get MethodBody into most available string result? Like: using Mono.Cecil; using Mono.Cecil.Cil; namespace my { public class Main { public Main() { // phseudo code, but doesnt work Console.Write( getMethod("HelloWorld").GetMethodBody().ToString() ); } public void HelloWorld(){ MessageBox.Show("Hiiiiiiiiii"); } } } 回答1: Start with reading your assembly: var path = "... path

Comparing Nested object properties using C#

走远了吗. 提交于 2020-08-04 18:20:31
问题 I have a method which compares two objects and returns a list of all the property names which are different. public static IList<string> GetDifferingProperties(object source, object target) { var sourceType = source.GetType(); var sourceProperties = sourceType.GetProperties(); var targetType = target.GetType(); var targetProperties = targetType.GetProperties(); var properties = (from s in sourceProperties from t in targetProperties where s.Name == t.Name && s.PropertyType == t.PropertyType &&

Comparing Nested object properties using C#

萝らか妹 提交于 2020-08-04 18:19:43
问题 I have a method which compares two objects and returns a list of all the property names which are different. public static IList<string> GetDifferingProperties(object source, object target) { var sourceType = source.GetType(); var sourceProperties = sourceType.GetProperties(); var targetType = target.GetType(); var targetProperties = targetType.GetProperties(); var properties = (from s in sourceProperties from t in targetProperties where s.Name == t.Name && s.PropertyType == t.PropertyType &&

Comparing Nested object properties using C#

倖福魔咒の 提交于 2020-08-04 18:19:09
问题 I have a method which compares two objects and returns a list of all the property names which are different. public static IList<string> GetDifferingProperties(object source, object target) { var sourceType = source.GetType(); var sourceProperties = sourceType.GetProperties(); var targetType = target.GetType(); var targetProperties = targetType.GetProperties(); var properties = (from s in sourceProperties from t in targetProperties where s.Name == t.Name && s.PropertyType == t.PropertyType &&

C# reflection and instantiation - is there a way to do Activator.CreateInstance(myType){ X = x }?

拈花ヽ惹草 提交于 2020-07-30 05:33:31
问题 I'm not sure of the terminology for this kind of code, but I want to know if it's possible to instantiate variables after the parentheses, but whilst using reflection. I have a map which gets loaded from an XML file. This is a collection of (int X, int Y, string S) where the X,Y is the position of some terrain, and S is a string representing the type of the terrain. I have a dictionary to pass between the strings and the relevant types; for example one key-value pair might be "Tree", typeof

Cannot get companion object given a class name

ぃ、小莉子 提交于 2020-07-22 21:35:47
问题 Based on another post I am trying to get a companion object for a class given the class name. But I cannot get it to compile: val clazz = Class.forName(className) val rootMirror = scala.reflect.runtime.universe.runtimeMirror(clazz.getClassLoader) val classSymbol = rootMirror.classSymbol(clazz) val classMirror = rootMirror.reflectClass(classSymbol) val moduleMirror = classMirror.companion.get But the last line gives me the following compiler error: - value companion is not a member of reflect

Get name of Enum instance [closed]

旧巷老猫 提交于 2020-07-21 06:07:53
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Improve this question Say I have this enum: public enum MyEnum{ ValueOne = 1, ValueTwo = 2, ValueThree = 3 } And then this field/variable: public MyEnum myEnumInstance = MyEnum.ValueTwo; I need to get the name of myEnumInstance via reflection from another class . I tried:

Get name of Enum instance [closed]

烈酒焚心 提交于 2020-07-21 06:07:39
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Improve this question Say I have this enum: public enum MyEnum{ ValueOne = 1, ValueTwo = 2, ValueThree = 3 } And then this field/variable: public MyEnum myEnumInstance = MyEnum.ValueTwo; I need to get the name of myEnumInstance via reflection from another class . I tried: