Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve property info from dynamic by Reflection
    primarykey
    data
    text
    <p><strong>ADDED:</strong> What I want to do. I have something, for example, DataReader, from which I want to create IEnumerable of objects with members as in DataReader. So, in design and compile time I don't know how many properties will be in my dynamic object (depends on how many columns contains in DataReader) and it names. AND I need to create such dynamic anonymous object with properties with right names and values, to get this properties in future by reflection...</p> <p>I thought about DynamicObject and that's what I've done:</p> <p>I have DynamicObject class:</p> <pre><code>using System.Collections.Generic; using System.Dynamic; using System.Linq; namespace makarov.ReportManager.InternalLogic { public class DataReaderParcer : DynamicObject { private readonly Dictionary&lt;string, object&gt; m_properties; public DataReaderParcer() { m_properties = new Dictionary&lt;string, object&gt;(); } public bool SetMember(string name, object value) { if (m_properties.ContainsKey(name)) m_properties.Remove(name); m_properties.Add(name, value); return true; } public override bool TrySetMember(SetMemberBinder binder, object value) { return SetMember(binder.Name, value); } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = null; if (m_properties.ContainsKey(binder.Name)) result = m_properties[binder.Name]; return m_properties.ContainsKey(binder.Name); } public override IEnumerable&lt;string&gt; GetDynamicMemberNames() { return from p in m_properties select p.Key; } } </code></pre> <p>}</p> <p>And here is the usage:</p> <pre><code> dynamic dd = new DataReaderParcer(); dd.MyMember= 3; dd.YourMember= "hello"; </code></pre> <p>How to retrieve PropertyInfo[] of this object using Reflection in another method? Something like this <code>dd.GetType().GetProperties()</code> doesn't work correctly because DataReaderParcer don't have any properties.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload