reflection

C#: Getting maximum and minimum values of arbitrary properties of all items in a list

人盡茶涼 提交于 2019-12-31 10:36:12
问题 I have a specialized list that holds items of type IThing : public class ThingList : IList<IThing> {...} public interface IThing { Decimal Weight { get; set; } Decimal Velocity { get; set; } Decimal Distance { get; set; } Decimal Age { get; set; } Decimal AnotherValue { get; set; } [...even more properties and methods...] } Sometimes I need to know the maximum or minimum of a certain property of all the things in the list. Because of "Tell don't ask" we let the List figure it out: public

Dynamically rethrowing self-defined C++ exceptions as Python exceptions using SWIG

眉间皱痕 提交于 2019-12-31 10:31:16
问题 Situation I want to create a Python language binding for a C++ API using SWIG. Some of the API functions may throw exceptions. The C++ application has a hierarchy of self-defined exceptions, like this example: std::exception -> API::Exception -> API::NetworkException -> API::TimeoutException -> API::UnreachableException -> API::InvalidAddressException The desired behavior is as follows: All exception types should have a matching Python class as wrapper . These wrapper classes should be valid

Java: Getting the properties of a class to construct a string representation

一笑奈何 提交于 2019-12-31 09:21:19
问题 Let's say I have a class like this (and also further assume that all the private variables: public class Item { private String _id = null; private String _name = null; private String _description = null; ... } Now, if I want to build a toString() representation of this class, I would do something like this inside the Item class: @Override public String toString() { return (_id + " " + _name + " " + _description); } But what if I have say 15 private variables inside the class? Do I have to

Why does a protected android:onClick method in Activity actually work?

只谈情不闲聊 提交于 2019-12-31 09:09:10
问题 Suppose you define android:onClick="doClick" in your Activity as protected void doClick(View view) { } The documentation states that This name must correspond to a public method that takes exactly one parameter of type View. This is a given requirement of the underlying Class.getMethod() method, which only finds public methods as the documentation states that it Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

How to know if a PropertyInfo is a collection

天涯浪子 提交于 2019-12-31 08:32:59
问题 Below is some code I use to get the initial state of all public properties in a class for IsDirty checking. What's the easiest way to see if a property is IEnumerable? Cheers, Berryl protected virtual Dictionary<string, object> _GetPropertyValues() { return _getPublicPropertiesWithSetters() .ToDictionary(pi => pi.Name, pi => pi.GetValue(this, null)); } private IEnumerable<PropertyInfo> _getPublicPropertiesWithSetters() { return GetType().GetProperties().Where(pi => pi.CanWrite); } UPDATE What

Get the name of the current class and create a txt with that name [closed]

佐手、 提交于 2019-12-31 07:52:26
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 days ago . Especially I need the program to create a text file. The name of the text file to be the class name. I tried this but does not work: String className = this.getClass().getName(); File file = new File("C:\\" + className + ".txt"); 回答1: Although you are creating a File object, you are not telling it to create it in

Get the name of the current class and create a txt with that name [closed]

不羁岁月 提交于 2019-12-31 07:50:05
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 days ago . Especially I need the program to create a text file. The name of the text file to be the class name. I tried this but does not work: String className = this.getClass().getName(); File file = new File("C:\\" + className + ".txt"); 回答1: Although you are creating a File object, you are not telling it to create it in

How to get a Type from a C# type name string?

狂风中的少年 提交于 2019-12-31 06:54:05
问题 I've been looking at return values for Type.Namespace , Type.Name , Type.FullName , and Type.AssemblyQualifiedName . There are inconsistencies. For an inner class like ConsoleApplication8.Program+InnerClass , Namespace returns ConsoleApplication8 and Name returns InnerClass , omitting Program , so concatenating Type.NameSpace and Type.Name would be an incomplete representation of the class name (just as an example). Even the FullName property is inconsistent. Although it omits assembly name

How to get a Type from a C# type name string?

两盒软妹~` 提交于 2019-12-31 06:53:47
问题 I've been looking at return values for Type.Namespace , Type.Name , Type.FullName , and Type.AssemblyQualifiedName . There are inconsistencies. For an inner class like ConsoleApplication8.Program+InnerClass , Namespace returns ConsoleApplication8 and Name returns InnerClass , omitting Program , so concatenating Type.NameSpace and Type.Name would be an incomplete representation of the class name (just as an example). Even the FullName property is inconsistent. Although it omits assembly name

is there a way to execute a function when I have its name in a string [duplicate]

北慕城南 提交于 2019-12-31 05:59:11
问题 This question already has answers here : How to execute a JavaScript function when I have its name as a string (33 answers) Closed 5 years ago . Consider I have a name of a function which does not require any argument in a var - var fn = "foo"; Can I execute it in some or similar like this - eval(fn); It does not work. Please suggest. My definition of function will look like this - function foo() { ....do something.... } 回答1: try this eval(fn)(); or this eval(fn + "()"); 回答2: Please do not