473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Expando object - adding dynamic properties

Not sure if this is possible or not but I'd like to have an ExpandoObject (.Net 4.0) that I can add properties to without knowing the property name until runtime. I have tried a couple of things but have had no luck yet. Can I do this? Here's a failed attempt:

Expand|Select|Wrap|Line Numbers
  1.             dynamic _Item = new ExpandoObject();
  2.  
  3.             foreach (DataRow r in dtIT.Rows)
  4.             {
  5.  
  6.                     //assign any matching column names to object properties
  7.                 foreach (DataColumn c in dtIT.Columns)
  8.                 {
  9.                     {
  10.                             _Item.c.ColumnName = r[c.ColumnName];
  11. ...
  12.  
Any suggestions? Thanks!
May 28 '10 #1

✓ answered by tlhintoq

Matt: o in the end instead of _Item.c.descrip, I want to have _Item.descrip
Then stop making an _Item.c. layer. If you only want _Item.descrip then only make _Item.descrip Its your code that is generating that hierarchy.
Expand|Select|Wrap|Line Numbers
  1. _Item.c.ColumnName = r[c.ColumnName];
Just because you have to reference the DataColumn C on the right side of the equasion doesn't force you to use it on the left.

If you want to do this, go right ahead

Expand|Select|Wrap|Line Numbers
  1. _Item.descrip = r.Columns[2];]
Now the name doesn't matter, so long as the third column is the description.

But if you are trying to somehow magically map an unknown and changing number of columns with unknown and changing names, to known properties... I don't see a way you can always be certain. *Some* rules have to govern any system. You have to know either the names or their positions to be sure you are getting what you expect to get.

You could search for a column whose name starts with "Descrip", and match to anything from 'Descript" to "Description" so long as the start is right.

6 5439
tlhintoq
3,525 Expert 2GB
It looks like you made a new _item but never made a new .c inside that _item.

So when you try to assign something to _item.c.columnname - it doesn't exist.

try this:

Expand|Select|Wrap|Line Numbers
  1. dynamic _Item = new ExpandoObject();
  2. _Item.c = new DataColumn();
Now you should have a DataColumn inside your _Item object that you can assign to.

Other examples I've seen just make a new ExpandoObject inside the parent ExpandoObject

Expand|Select|Wrap|Line Numbers
  1. dynamic _Item = new ExpandoObject();
  2. _Item.c = new ExpanoObject();
  3. _Item.c.Name = "Bob";
Etc.
May 28 '10 #2
@tlhintoq
Thanks, but that isn't exactly what I'm trying to do.
For example, there is a DataColumn called descrip of type string in the DataTable. I want to create _Item.descrip and populate with the text value of the current r.descrip without explicitly declaring _Item.descrip. Can I do that?
May 28 '10 #3
tlhintoq
3,525 Expert 2GB
You have to make a thing, before you can assign value to a thing. ExpandoObjects aren't magical, just easier.

But you can make a thing at run-time, then give it value. You don't have to pre-define it in code at design-time. Isn't that what you are trying to accomplish?

Expand|Select|Wrap|Line Numbers
  1. dynamic _Item = new ExpandoObject();
  2.  
  3.             foreach (DataRow r in dtIT.Rows)
  4.             {
  5.  
  6.                     //assign any matching column names to object properties
  7.                 foreach (DataColumn c in dtIT.Columns)
  8.                 {
  9.                     {
  10. // add this line
  11. _Item.c = new DataColumn();
  12. // Now you have a thing you can give value to
  13.                             _Item.c.ColumnName = r[c.ColumnName];
May 28 '10 #4
Right. I'm just trying to see if I can dynamically create the properties, so in the end I have one expandoobject per row in this table with one property for each field in that row with the same name as the column populated with the value in the datarow.
So in the end instead of _Item.c.descrip, I want to have _Item.descrip and the value of item.descrip would equal the value of r["descrip"]. Then in the future if the descrip column name changes to "description", I'd still like it to work without having to modify it. So if the column name changed then _Item.description = r["description"].
Sorry if I'm not being clear (or asking the impossible - I'd accept that answer if that was the case).
May 28 '10 #5
tlhintoq
3,525 Expert 2GB
Matt: o in the end instead of _Item.c.descrip, I want to have _Item.descrip
Then stop making an _Item.c. layer. If you only want _Item.descrip then only make _Item.descrip Its your code that is generating that hierarchy.
Expand|Select|Wrap|Line Numbers
  1. _Item.c.ColumnName = r[c.ColumnName];
Just because you have to reference the DataColumn C on the right side of the equasion doesn't force you to use it on the left.

If you want to do this, go right ahead

Expand|Select|Wrap|Line Numbers
  1. _Item.descrip = r.Columns[2];]
Now the name doesn't matter, so long as the third column is the description.

But if you are trying to somehow magically map an unknown and changing number of columns with unknown and changing names, to known properties... I don't see a way you can always be certain. *Some* rules have to govern any system. You have to know either the names or their positions to be sure you are getting what you expect to get.

You could search for a column whose name starts with "Descrip", and match to anything from 'Descript" to "Description" so long as the start is right.
May 28 '10 #6
@tlhintoq
Thanks again!
May 28 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: listerofsmeg | last post by:
Newbie here. I want to use a .config file with my simple console app, but no matter what I do I cannot get the "dynamic" rollout in my properties window. In the end I gave up and coded the...
3
by: Guy Harwood | last post by:
Hi, I have designed a textbox that inherits from the System.Windows.Forms.Textbox control. when the control is readonly the back color changes to a light blue to indicate that it is frozen. ...
4
by: Richard Abraham | last post by:
I have created a new class ServiceTimer based upon the .NET System.Timers.Timer class. This has a new property CollectionIntervalMinutes which I expected to be added to the 'Behavior' section of the...
1
by: jm | last post by:
I am using (trying) dynamic properties to store a database connection. However, I don't have a connection object on the form. I just do a OdbcConnection in my code. So I can't use the VS IDE to...
2
by: noel.phillips | last post by:
Hi, I want to achieve a system that has a number of objects with what I would call 'core' properties - that is properties defined at design time which correspond (directly or otherwise) to a...
1
by: davidw | last post by:
I found it seems there is a way to create a class with dynamic properties, I don't really need define each property? In ASP.NET, Profile is created with all profile items in web.config as its...
3
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
5
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
0
by: vnu | last post by:
Hi, Hi, I am working in .net emvironment using vs2005 and vb.net as the language. In one of my project, I need to display the properties of a com object which has dynamic properties(late bind...
1
by: RSH | last post by:
Hi, I have a situation where I need to add several "Hidden" properties to list items in a dropdownlist. By default the DropDownList item has two properties with regards to the listitems...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.