Automatically create properties

  • Thread starter Thread starter barry
  • Start date Start date
B

barry

Is there any way that I can have a piece of code create properties and
give them names for either the same class or a different class.

For example (this problem could be solved with inheritance but its
just to help present my question):

If I have a class called "shape" and I want a seperate piece of code
to give the appropriate properties for the object such as radius,
length, height, etc. based on the type of shape it is.

Thank you.
 
barry said:
Is there any way that I can have a piece of code create properties and
give them names for either the same class or a different class.

For example (this problem could be solved with inheritance but its
just to help present my question):

If I have a class called "shape" and I want a seperate piece of code
to give the appropriate properties for the object such as radius,
length, height, etc. based on the type of shape it is.

Thank you.

Hi,
Maybe, indexer is that you want.
Example:
public class Shape
{
private Hashtable additionalProperties = new Hashtable();
public object this[string name]
{
get
{
return additionalProperties[name];
}
set
{
if (additionalProperties.ContainsKey(name))
additionalProperties[name] = value;
else
additionalProperties.Add(name, value);
}
}
}

and using:
Shape obj = new Shape();
obj["Radius"] = 15;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top