digitalmars.D.learn - Is it possible using reflection or similar to extract only public
- Gary Willoughby (4/4) Aug 06 2013 Is it possible using reflection or similar to extract only public
- Marek Janukowicz (5/9) Aug 06 2013 See traits: http://dlang.org/traits.html
- Jacob Carlborg (4/6) Aug 07 2013 And "allMembers" and "derivedMembers".
- Gary Willoughby (2/10) Aug 07 2013 I thought using __traits(...) was frowned upon?
- Adam D. Ruppe (5/6) Aug 07 2013 They want to give them phobos wrappers that might be a little
- H. S. Teoh (83/87) Aug 06 2013 The following code demonstrates how you can do this:
Is it possible using reflection or similar to extract only public method names from classes? I'm thinking how i would go about writing a unit test/mocking framework, investigating how i can gather information about such things before i manipulate them.
Aug 06 2013
Gary Willoughby wrote:Is it possible using reflection or similar to extract only public method names from classes? I'm thinking how i would go about writing a unit test/mocking framework, investigating how i can gather information about such things before i manipulate them.See traits: http://dlang.org/traits.html Look for: getProtection, getVirtualFunctions, getVirtualMethods. -- Marek Janukowicz
Aug 06 2013
On 2013-08-07 00:57, Marek Janukowicz wrote:See traits: http://dlang.org/traits.html Look for: getProtection, getVirtualFunctions, getVirtualMethods.And "allMembers" and "derivedMembers". -- /Jacob Carlborg
Aug 07 2013
On Tuesday, 6 August 2013 at 22:55:49 UTC, Marek Janukowicz wrote:Gary Willoughby wrote:I thought using __traits(...) was frowned upon?Is it possible using reflection or similar to extract only public method names from classes? I'm thinking how i would go about writing a unit test/mocking framework, investigating how i can gather information about such things before i manipulate them.See traits: http://dlang.org/traits.html Look for: getProtection, getVirtualFunctions, getVirtualMethods.
Aug 07 2013
On Wednesday, 7 August 2013 at 11:47:27 UTC, Gary Willoughby wrote:I thought using __traits(...) was frowned upon?They want to give them phobos wrappers that might be a little prettier, but other than beauty/ugliness there's no reason not to use it.
Aug 07 2013
On Tue, Aug 06, 2013 at 11:41:35PM +0200, Gary Willoughby wrote:Is it possible using reflection or similar to extract only public method names from classes? I'm thinking how i would go about writing a unit test/mocking framework, investigating how i can gather information about such things before i manipulate them.The following code demonstrates how you can do this: import std.stdio; class Base { private int x; public int y; this() {} private void privMethod() {} public void method() {} } class Derived : Base { public override void method() {} public void derivedMethod() {} private void privDerivedMethod() {} } void showAllMethods(C)(C obj) { writeln("All members:"); foreach (field; __traits(allMembers, C)) { static if (is(typeof(__traits(getMember, obj, field)) T == function)) { auto prot = __traits(getProtection, __traits(getMember, obj, field)); writefln("\t(%s) %s", prot, field); } } } void showDerivedMethods(C)(C obj) { writeln("\nDerived members:"); foreach (field; __traits(derivedMembers, C)) { static if (is(typeof(__traits(getMember, obj, field)) T == function)) { auto prot = __traits(getProtection, __traits(getMember, obj, field)); writefln("\t(%s) %s", prot, field); } } } string[] getPublicMethods(C)(C obj) { string[] methods; foreach (field; __traits(allMembers, C)) { static if (is(typeof(__traits(getMember, obj, field)) == function) && __traits(getProtection, __traits(getMember, obj, field)) == "public") { methods ~= field; } } return methods; } void main() { auto d = new Derived(); showAllMethods(d); showDerivedMethods(d); writeln("All public methods:"); writeln(getPublicMethods(d)); } The output is: All members: (public) method (public) derivedMethod (private) privDerivedMethod (public) __ctor (private) privMethod (public) toString (public) toHash (public) opCmp (public) opEquals (public) factory Derived members: (public) method (public) derivedMethod (private) privDerivedMethod (public) __ctor All public methods: ["method", "derivedMethod", "__ctor", "toString", "toHash", "opCmp", "opEquals", "factory"] Hope this helps! T -- Designer clothes: how to cover less by paying more.
Aug 06 2013