dynamically created object properties

N

Neil Chambers

I would like to pass an array of strings to an object and have the object
members built using the array items as property names. How would I go about
doing this?

Example:

public class myDynamicObject
{
public myDynamicObject(string[] memberNames)
{
foreach (string s in memberNames)
{
//make s a public property of the object
//assign a value to s
}
}
}

Many thanks!
neil
 
J

Jon Skeet [C# MVP]

I would like to pass an array of strings to an object and have the object
members built using the array items as property names. How would I go about
doing this?

You can't - types don't change on the fly; you can't add a property to
an object.

What's the use case here? Is there anything that a simple
Dictionary<string, object> wouldn't solve?

Jon
 
M

Marc Gravell

Can you clarify what you want to do?

If you want to assign values to already-existing properties, then
reflection is your friend; or System.ComponentModel:

TypeDescriptor.GetProperties(obj)["PropName"].SetValue(obj, newValue);

is broadly equivalent to:
obj.PropName = newValue;

If the properties don't already exist and you want runtime-only
properties, then you are into a more complex area of custom type
descriptors... it can work, but is mainly useful for data-binding to UIs
etc. I can provide more info, but I'd prefer to know that this is what
you meant, first...

Marc
 
C

Ciaran O''Donnell

This isnt a simple thing to do in C# as it isnt a dynamic language. You would
need to use the classes in the Reflection.Emit language to build the class.
This is complex stuff and I havent found a need to ever do it.
Google Reflection.Emit and you should come up with some answers
If you want, let us know what you are trying to acheive in terms of the big
picture as there might be and easier way
 
N

Neil Chambers

First off - thanks for the insight.

I am coming to C# from a PowerShell/scripting background rather than OO -
which might explain where my thought processes are at ;)

I am constructing a class which I can use to pull information from Active
Directory using the DirectoryServices namespace. I want to use dynamic
properties for the search filter and propertiesToLoad. The return type is
System.DirectoryServices.SearchResult (the AD Object I'm after) which
contains one or more ResultPropertyCollection (multivalue properties).

I want to obscure the complexity of the SearchResult by converting the
ResultPropertyCollection(s) to single valued value types such that the final
return object has a set of simple properties to make asp.net data control
binding easy - and I wan't to do that without knowing which properties will
be returned.

Currently I have a class which defines a template for the return object
within which I do the type conversion (so I shoe horn the SearchResult into
a custom object with a set of predefined properties) - This will do for now
but it doesn't make the code very resilient to change - or nice to look at.

I am sure there is an easier way to do this! - I haven't looked at the
dictionary approach yet...

Cheers!
Neil


Ciaran O''Donnell said:
This isnt a simple thing to do in C# as it isnt a dynamic language. You
would
need to use the classes in the Reflection.Emit language to build the
class.
This is complex stuff and I havent found a need to ever do it.
Google Reflection.Emit and you should come up with some answers
If you want, let us know what you are trying to acheive in terms of the
big
picture as there might be and easier way

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Neil Chambers said:
I would like to pass an array of strings to an object and have the object
members built using the array items as property names. How would I go
about
doing this?

Example:

public class myDynamicObject
{
public myDynamicObject(string[] memberNames)
{
foreach (string s in memberNames)
{
//make s a public property of the object
//assign a value to s
}
}
}

Many thanks!
neil
 
I

Ignacio Machin ( .NET/ C# MVP )

I would like to pass an array of strings to an object and have the object
members built using the array items as property names. How would I go about
doing this?

Example:

public class myDynamicObject
{
public myDynamicObject(string[] memberNames)
{
foreach (string s in memberNames)
{
//make s a public property of the object
//assign a value to s
}
}

}

Many thanks!
neil

You cannot.
What you can do is having one property or one method taht return the
value you want.
you could have
string GetPropertyValue(string propertyName)
or if you do not mind using an indexer you can have:
string this[string propertyName]
n this case you access it by using
myInstance["propertyName"]

You can hold your property/values pairs in a dictionary.
 

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

Top