class

C# - How to check if namespace, class or method exists in C#?

我是研究僧i 提交于 2019-12-28 03:40:33
问题 I have a C# program, how can I check at runtime if a namespace, class, or method exists? Also, how to instantiate a class by using it's name in the form of string? Pseudocode: string @namespace = "MyNameSpace"; string @class = "MyClass"; string method= "MyMEthod"; var y = IsNamespaceExists(namespace); var x = IsClassExists(@class)? new @class : null; //Check if exists, instantiate if so. var z = x.IsMethodExists(method); 回答1: You can use Type.GetType(string) to reflect a type. GetType will

Instantiate a class from its textual name

旧街凉风 提交于 2019-12-28 02:04:59
问题 Don't ask me why but I need to do the following: string ClassName = "SomeClassName"; object o = MagicallyCreateInstance("SomeClassName"); I want to know how many ways there are to do this is and which approach to use in which scenario. Examples: Activator.CreateInstance Assembly.GetExecutingAssembly.CreateInstance("") Any other suggestions would be appreciated This question is not meant to be an open ended discussion because I am sure there are only so many ways this can be achieved. 回答1:

“This” within es6 class method [duplicate]

末鹿安然 提交于 2019-12-28 01:55:11
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 3 years ago . For some reason I'm getting weird values for "this" in my es6 class... 'use strict'; class Clicker { constructor(element) { this.count = 0; this.elem = element; this.elem.addEventListener('click', this.click); // logs Clicker { count:0, elem: button#thing} as expected console.log(this); } click() { // logs <button id="thing">...</button> as unexpected... console.log

Mixing class and struct

耗尽温柔 提交于 2019-12-28 01:20:10
问题 I'm well aware of the difference between class and struct, however I'm struggling to authoritatively say if this is well defined: // declare foo (struct) struct foo; // define foo (class) class foo { }; // instance of foo, claiming to be a struct again! Well defined? struct foo bar; // mixing class and struct like this upsets at least one compiler (names are mangled differently) const foo& test() { return bar; } int main() { test(); return 0; } If this is undefined behaviour can someone point

Objective-C get a class property from string

丶灬走出姿态 提交于 2019-12-27 22:06:31
问题 I've heard a number of similar questions for other languages, but I'm looking for a specific scenario. My app has a Core Data model called "Record", which has a number of columns/properties like "date, column1 and column2". To keep the programming clean so I can adapt my app to multiple scenarios, input fields are mapped to a Core Data property inside a plist (so for example, I have a string variable called "dataToGet" with a value of 'column1'. How can I retrieve the property "column1" from

Objective-C get a class property from string

此生再无相见时 提交于 2019-12-27 22:05:29
问题 I've heard a number of similar questions for other languages, but I'm looking for a specific scenario. My app has a Core Data model called "Record", which has a number of columns/properties like "date, column1 and column2". To keep the programming clean so I can adapt my app to multiple scenarios, input fields are mapped to a Core Data property inside a plist (so for example, I have a string variable called "dataToGet" with a value of 'column1'. How can I retrieve the property "column1" from

Are classes objects in Objective-C?

允我心安 提交于 2019-12-27 20:12:12
问题 okay, so I understand that an object is an instance of a class that must be allocated and initialized, but are classes themselves objects? I know when you create a new class it is an instance of something else, like NSObject. So, if this makes it a class, then objects can hold not only variables and methods, but other objects as well, right? Sorry, this is probably really basic, but I am reading two books about cocoa and xcode and this point is a little unclear (probably because of my lack of

Class methods as event handlers in JavaScript?

独自空忆成欢 提交于 2019-12-27 19:24:12
问题 Is there a best-practice or common way in JavaScript to have class members as event handlers? Consider the following simple example: <head> <script language="javascript" type="text/javascript"> ClickCounter = function(buttonId) { this._clickCount = 0; document.getElementById(buttonId).onclick = this.buttonClicked; } ClickCounter.prototype = { buttonClicked: function() { this._clickCount++; alert('the button was clicked ' + this._clickCount + ' times'); } } </script> </head> <body> <input type

Class methods as event handlers in JavaScript?

烈酒焚心 提交于 2019-12-27 19:21:11
问题 Is there a best-practice or common way in JavaScript to have class members as event handlers? Consider the following simple example: <head> <script language="javascript" type="text/javascript"> ClickCounter = function(buttonId) { this._clickCount = 0; document.getElementById(buttonId).onclick = this.buttonClicked; } ClickCounter.prototype = { buttonClicked: function() { this._clickCount++; alert('the button was clicked ' + this._clickCount + ' times'); } } </script> </head> <body> <input type

How to create two classes in C++ which use each other as data?

删除回忆录丶 提交于 2019-12-27 16:51:05
问题 I'm looking to create two classes, each of which contains an object of the other class type. How can I do this? If I can't do this, is there a work-around, like having each class contain a pointer to the other class type? Thanks! Here's what I have: File: bar.h #ifndef BAR_H #define BAR_H #include "foo.h" class bar { public: foo getFoo(); protected: foo f; }; #endif File: foo.h #ifndef FOO_H #define FOO_H #include "bar.h" class foo { public: bar getBar(); protected: bar b; }; #endif File: