Thread overview | |||||
---|---|---|---|---|---|
|
March 29, 2007 attributes | ||||
---|---|---|---|---|
| ||||
Hi all! is there a way to get all attributes and their types in a class at runtime? Comming from the c# world I'm missing something like meta tags [attributes] - something like reflection. I wonder how this issue can be solved in d? |
March 29, 2007 Re: attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to macky | macky wrote:
> Hi all!
>
> is there a way to get all attributes and their types in a class at runtime? Comming from the c# world I'm missing something like meta tags [attributes] - something like reflection. I wonder how this issue can be solved in d?
Perhaps something like this:
import std.stdio;
class A {
int a;
float b;
C c;
}
class C {
int i;
}
void main() {
A a = new A;
foreach (i, attrib; a.tupleof) {
writefln("attribute ", i, " is of type ", typeid(typeof(attrib)), " and value ", attrib);
}
}
Output:
attribute 0 is of type int and value 0
attribute 1 is of type float and value nan
attribute 2 is of type attribList.C and value null
|
March 30, 2007 Re: attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bradley Smith | Thanx, this seems like a nice solution. I will look into it!
Bradley Smith Wrote:
> macky wrote:
> > Hi all!
> >
> > is there a way to get all attributes and their types in a class at runtime? Comming from the c# world I'm missing something like meta tags [attributes] - something like reflection. I wonder how this issue can be solved in d?
>
> Perhaps something like this:
>
> import std.stdio;
>
> class A {
> int a;
> float b;
> C c;
> }
>
> class C {
> int i;
> }
>
> void main() {
> A a = new A;
> foreach (i, attrib; a.tupleof) {
> writefln("attribute ", i, " is of type ", typeid(typeof(attrib)), "
> and value ", attrib);
> }
> }
>
> Output:
>
> attribute 0 is of type int and value 0
> attribute 1 is of type float and value nan
> attribute 2 is of type attribList.C and value null
|
Copyright © 1999-2021 by the D Language Foundation