M
Marco
I can't convert data to runtime.
class User
{
public User(string first,string last)
{
this.first=first;
this.last=last;
}
public string first;
public string last;
}
class Environment
public IDictionary dict;
public Environment()
{
dict=new Hashtable();
}
public void bind(String name,Object data)
{
Object d=data;
dict.Add(name,d);
}
}
class Template
{
String Query; // Query is "repeat(people) people.first..."
public String gen(Environment env)
{
Object data=env.dict["people"]
// ....
}
}
Main()
{
user[] users=new User{
new User("Jack", "Forester")
new User("Tony", "Corallo")
}
Template tab=Template.parse("repeat(people) people.first...")
Environment env=new Environment();
env.bind("people",users);
String table=tab.gen(env);
}
The purpose is realize a template engine.
My input is :
"repeat(people) people.first..."
My output must be :
Jack
Tony
The method env.bind() can take a generic collection.
In the method gen of template class I have to read and print all
attribute of generic collection.
For example:
in this case I have to read "users" and print the "first" attribute of
all user element.
The gen output must be.
Jack
Tony
My problem is:
After:
Object data=env.dict["people"]
how can I convert data to user[].
I have to do this at runtime.
I can't do user[] User=(user[]) data;
because the collection is generic and I don't know the type.
How can I use reflection to do it?
class User
{
public User(string first,string last)
{
this.first=first;
this.last=last;
}
public string first;
public string last;
}
class Environment
public IDictionary dict;
public Environment()
{
dict=new Hashtable();
}
public void bind(String name,Object data)
{
Object d=data;
dict.Add(name,d);
}
}
class Template
{
String Query; // Query is "repeat(people) people.first..."
public String gen(Environment env)
{
Object data=env.dict["people"]
// ....
}
}
Main()
{
user[] users=new User{
new User("Jack", "Forester")
new User("Tony", "Corallo")
}
Template tab=Template.parse("repeat(people) people.first...")
Environment env=new Environment();
env.bind("people",users);
String table=tab.gen(env);
}
The purpose is realize a template engine.
My input is :
"repeat(people) people.first..."
My output must be :
Jack
Tony
The method env.bind() can take a generic collection.
In the method gen of template class I have to read and print all
attribute of generic collection.
For example:
in this case I have to read "users" and print the "first" attribute of
all user element.
The gen output must be.
Jack
Tony
My problem is:
After:
Object data=env.dict["people"]
how can I convert data to user[].
I have to do this at runtime.
I can't do user[] User=(user[]) data;
because the collection is generic and I don't know the type.
How can I use reflection to do it?