Entity Name reflector<p align=left>Hi All</p> <p align=left> </p> <p align=left>I have a Blog entity with a Blog_ID field. Is it possible for me to do some reflector to get the Blog_ID and return it as string?</p> <p align=left> </p> <p align=left>It is because I am building a selectlist but don't want to hard code like this.</p> <p align=left>new SelectList(blog,&quot;blog_ID&quot;,&quot;blog_Description&quot;);</p> <p align=left> </p> <p align=left>I want to make it strong-typed, is it possible?</p> <p align=left> </p> <p align=left>Regards</p> <p align=left>Alex</p>© 2009 Microsoft Corporation. All rights reserved.Fri, 03 Jul 2009 04:29:43 Z4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5hnchasshttp://social.msdn.microsoft.com/Profile/en-US/?user=hnchassEntity Name reflector<p align=left>Hi All</p> <p align=left> </p> <p align=left>I have a Blog entity with a Blog_ID field. Is it possible for me to do some reflector to get the Blog_ID and return it as string?</p> <p align=left> </p> <p align=left>It is because I am building a selectlist but don't want to hard code like this.</p> <p align=left>new SelectList(blog,&quot;blog_ID&quot;,&quot;blog_Description&quot;);</p> <p align=left> </p> <p align=left>I want to make it strong-typed, is it possible?</p> <p align=left> </p> <p align=left>Regards</p> <p align=left>Alex</p>Fri, 04 Jul 2008 07:33:23 Z2008-07-05T05:57:40Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#9d4f3758-b5a2-4687-a185-e38372777c00http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#9d4f3758-b5a2-4687-a185-e38372777c00Vasco Oliveirahttp://social.msdn.microsoft.com/Profile/en-US/?user=Vasco%20OliveiraEntity Name reflector<p>Yes, it's possible.</p> <p align=left> </p> <p align=left>In my opinion, the best way is to check EntityKeyMembers array of the EntityKey property of the Blog entity. You can then get a name list if your key fields.</p> <p align=left> </p> <p align=left>On the other hand you can use reflection on the Blog entity and iterate through all properties to see their CustomAttributes and check if it's of type EdmScalarPropertyAttribute and if so, see if EntityKeyProperty is true. For example, to get a list of all key properties:</p> <p align=left> </p> <p align=left> </p><font color="#0000ff" size=1><font color="#0000ff" size=1> <p>foreach</font></font><font size=1> (</font><font color="#2b91af" size=1><font color="#2b91af" size=1>PropertyInfo</font></font><font size=1> prop </font><font color="#0000ff" size=1><font color="#0000ff" size=1>in</font></font><font size=1> typeof(Blog).GetProperties())</p> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></font><font color="#0000ff" size=1><font color="#0000ff" size=1>object</font></font><font size=1>[] attrs = prop.GetCustomAttributes(</font><font color="#0000ff" size=1><font color="#0000ff" size=1>false</font></font><font size=1>);</p> <p></font><font color="#0000ff" size=1><font color="#0000ff" size=1>foreach</font></font><font size=1> (</font><font color="#0000ff" size=1><font color="#0000ff" size=1>object</font></font><font size=1> obj </font><font color="#0000ff" size=1><font color="#0000ff" size=1>in</font></font><font size=1> attrs)</p> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></font><font color="#0000ff" size=1><font color="#0000ff" size=1>if</font></font><font size=1> (obj.GetType() == </font><font color="#0000ff" size=1><font color="#0000ff" size=1>typeof</font></font><font size=1>(</font><font color="#2b91af" size=1><font color="#2b91af" size=1>EdmScalarPropertyAttribute</font></font><font size=1>))</p> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></font><font color="#2b91af" size=1><font color="#2b91af" size=1>EdmScalarPropertyAttribute</font></font><font size=1> attr = (</font><font color="#2b91af" size=1><font color="#2b91af" size=1>EdmScalarPropertyAttribute</font></font><font size=1>)obj;</p> <p></font><font color="#0000ff" size=1><font color="#0000ff" size=1>if</font></font><font size=1> (attr.EntityKeyProperty)</p> <blockquote dir=ltr style="margin-right:0px"> <p>keyList.Add(prop.Name);</p></blockquote></blockquote> <p>}</p></blockquote> <p>}</p></blockquote> <p>}</p></font> <p align=left> </p> <p align=left> </p> <p align=left> </p> <p align=left>Hope it helps</p>Fri, 04 Jul 2008 09:05:50 Z2008-07-04T11:13:48Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#58787117-fa02-44a9-bc53-e5fd75ca6b87http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#58787117-fa02-44a9-bc53-e5fd75ca6b87Greg Bachratyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Greg%20BachratyEntity Name reflector<p align=left>Instead of using strings or reflection you could consider lambda expressions.</p> <p align=left>You can create a generic SelectList with this constructor:</p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>SelectList(T entity, Expression&lt;Func&lt;T,object&gt;&gt; field1, Expression&lt;Func&lt;T,object&gt;&gt; field2)</p> <p align=left> </p></div></div> <p align=left>Then you can use it like this with strong typing:</p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>new SelectList(blog, b=&gt;blog_ID, b=&gt;blog_Description);</p> <p align=left> </p></div></div> <p align=left>You don't even need to create a generic class if you don't want to and mine the corresponding strings from the expression trees above:</p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>static SelectList CreateList&lt;T&gt;(T entity, Expression&lt;Func&lt;T,object&gt;&gt; field1, Expression&lt;Func&lt;T,object&gt;&gt; field2) <p align=left>{</p> <p align=left>    string f1name = (field1.Body as MemberExpression).Member.Name;<br>    string f2name = (field2.Body as MemberExpression).Member.Name;</p> <p align=left>    return new SelectList(entity, f1name, f2name);<br>}</p> <p align=left> </p> <p align=left>SelectList.Create(blog, b=&gt;blog_ID, b=&gt;blog_Description);</p></div></div> <p align=left>Although this latter aproach will only throw a runtime exception if you use different expressions than the intended simple field access.</p>Fri, 04 Jul 2008 19:33:42 Z2008-07-05T05:57:40Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#73dfeefd-c7d9-4810-b16a-18c156cb3221http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#73dfeefd-c7d9-4810-b16a-18c156cb3221hnchasshttp://social.msdn.microsoft.com/Profile/en-US/?user=hnchassEntity Name reflector<p align=left>Excellent Solution!!! Thanks.</p> <p align=left> </p> <p align=left>However, My selectlist's original data type is a List and the code is as follows :-</p> <p align=left> </p> <p align=left>ViewData[&quot;Blog_TypeList&quot;] = WebUtility.CreateList (blog_TypeList.OrderBy(c =&gt; c.Seq), c =&gt; c.First().Blog_Type_ID, c =&gt; c.First().Description);</p> <p align=left> </p> <p align=left>Could I remove the First() from the code?</p> <p align=left><br>        </p>Sat, 05 Jul 2008 05:59:24 Z2008-07-05T05:59:24Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#be5fc3e7-9782-4a66-8fb6-7ab25c4bd008http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#be5fc3e7-9782-4a66-8fb6-7ab25c4bd008Greg Bachratyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Greg%20BachratyEntity Name reflector<p align=left><font face=Arial size=2>You can change the &quot;T entity&quot; parameter to &quot;IEnumerable&lt;T&gt; entities&quot;. The compiler will still deduce the generic parameter for the function.</font></p>Sat, 05 Jul 2008 07:59:40 Z2008-07-05T07:59:40Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#b89e4ee9-ef9a-45a1-beca-5999a947f328http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#b89e4ee9-ef9a-45a1-beca-5999a947f328hnchasshttp://social.msdn.microsoft.com/Profile/en-US/?user=hnchassEntity Name reflector<p align=left><font face=Arial size=2>Thanks. Cool. It works.</font></p> <p align=left> </p> <p align=left>Besides, just curious, for this solution:-</p> <p align=left> </p> <p align=left><span style="width:100%">Code Snippet</span></p> <p align=left>SelectList(T entity, Expression&lt;Func&lt;T,object&gt;&gt; field1, Expression&lt;Func&lt;T,object&gt;&gt; field2)</p> <p align=left> </p> <p align=left>Then you can use it like this with strong typing:</p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>new SelectList(blog, b=&gt;blog_ID, b=&gt;blog_Description);</p> <p align=left> </p></div></div> <div class=codeseg align=left> </div> <div class=codeseg align=left>Where do I put the constructor, modify the source code?</div> <div class=codeseg align=left> </div> <div class=codeseg align=left> </div>Sat, 05 Jul 2008 09:03:50 Z2008-07-05T09:03:50Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#8427ce4d-c9e5-4d27-ac40-3c1d3d99c3adhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4cfbfa0b-6c07-4c2e-b5f2-19fcc381a4f5#8427ce4d-c9e5-4d27-ac40-3c1d3d99c3adShimmy Weitzhandlerhttp://social.msdn.microsoft.com/Profile/en-US/?user=Shimmy%20WeitzhandlerEntity Name reflector<pre> public static string[] GetIdField&lt;TEntity&gt;() where TEntity : EntityObject { IEnumerable&lt;string&gt; ids = from p in typeof(TEntity).GetProperties() where (from a in p.GetCustomAttributes(false) where a is EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)a).EntityKeyProperty select true).FirstOrDefault() select p.Name; return ids.ToArray(); }</pre><hr class="sig">ShimmyFri, 03 Jul 2009 04:29:43 Z2009-07-03T04:29:43Z