Reflection (About)http://forums.asp.net/t/1726188.aspx/1?Reflection+About+Wed, 12 Oct 2011 18:28:41 -040017261884622041http://forums.asp.net/p/1726188/4622041.aspx/1?Reflection+About+Reflection (About) <p>What if I have an entity instantiated as: Products products = new Products();</p> <p>what if I had 30 more entities and I´d like to pass to the constructort a variable that can be recognized as an entity like:</p> <p>var comingentity = table.Name;<br> [some kind of conversion here ??? ]<br> <strong>comingentity&nbsp;</strong>myentity = new <strong>comingentity</strong>();</p> <p>&nbsp;</p> 2011-10-01T18:53:18-04:004622417http://forums.asp.net/p/1726188/4622417.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>Hi Topolov, not sure what you are tryign to achive?</p> 2011-10-02T13:38:56-04:004622657http://forums.asp.net/p/1726188/4622657.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>Hello topolov,</p> <p>I guess that you want to return a collection of a specific kind of Entity. So you can first define a function with two parameters:</p> <p>1) T entityentity model class.</p> <p>2) object collectionfor containing vlaues.</p> <p><strong>public void AutoGetValues&lt;T&gt;(T entity,object[]values) where T:TEntity</strong><br> <strong>{</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var results = entity.GetProperties();</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values = new object[results.Length];</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i=0;</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (PropertyInfo p in results)</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values[i&#43;&#43;] = p.GetValue(entity,null);</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong><br> <strong>}</strong></p> <p>3) And then call this method as:<br> <strong>object[] values = null;&nbsp;</strong><br> <strong>AutoGetValues&lt;ModelClass&gt;(ModelClassInstance, values);</strong></p> 2011-10-03T00:19:01-04:004625506http://forums.asp.net/p/1726188/4625506.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>Couldn´t make it work. Would you mind indicating me how to use with real life data like Northwind, Products, etc. I don´t know what to write in every step of the way. I have tried different combinations but still no luck</p> <p>It seems that it requires something else that you already have taht I don´t as to work fine</p> <p>I just copied and pasted the code in my OnInserting FormView event but it gives a lot of error messages</p> <p>&nbsp;</p> <p>Please</p> <p>&nbsp;</p> <p>----------------------</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Decker Dong - MSFT</h4> <p></p> <p>Hello topolov,</p> <p>I guess that you want to return a collection of a specific kind of Entity. So you can first define a function with two parameters:</p> <p>1) T entityentity model class.</p> <p>2) object collectionfor containing vlaues.</p> <p><strong>public void AutoGetValues&lt;T&gt;(T entity,object[]values) where T:TEntity</strong><br> <strong>{</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var results = entity.GetProperties();</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values = new object[results.Length];</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i=0;</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (PropertyInfo p in results)</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values[i&#43;&#43;] = p.GetValue(entity,null);</strong><br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong><br> <strong>}</strong></p> <p>3) And then call this method as:<br> <strong>object[] values = null;&nbsp;</strong><br> <strong>AutoGetValues&lt;ModelClass&gt;(ModelClassInstance, values);</strong></p> <p></p> </blockquote> <p></p> 2011-10-04T16:10:19-04:004625952http://forums.asp.net/p/1726188/4625952.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>Hello Topolov:)</p> <p>I've tipped you instead of doing all the things for you...</p> <p>By the way. Here I've told you:</p> <p>ModelClass is the model generated from real db tables, please use this instead.</p> 2011-10-05T02:36:40-04:004627058http://forums.asp.net/p/1726188/4627058.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>Sorry for asking but it is just because I don't understand</p> <p>I'm not a sloppy person but I just don't get it</p> <p>But it's ok. That had happened to me a lot here in this forum. From where I come we say that it's much bertter to ask twice than look like a fool the rest of your life so it seems natural for us asking when we reach a dead end road</p> <p>If I were supposed to know as much as I should on the .ASP. Net Dynamic Data framework then I wouldn't come to ask here</p> <p>Problem is that Dynamic Data literature is not really available from the manufacturer and to be true all the help environment of the official web site lacks of true examples and power</p> <p>You must go somehwre else to find the answers</p> <p>I have asked on this several times with no luck</p> <p>How difficult can taht be to convert the word &quot;Products&quot; into something which .Net understands as the &quot;Products&quot; entity instead</p> <p>There are many languages where this is done with no major problem at all but in .Net thins must go hand by hand with Intellisense and there you have the problem: not knowing what to do to make a simple substitution</p> <p>That's sad</p> 2011-10-05T18:03:47-04:004627295http://forums.asp.net/p/1726188/4627295.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>Hello Topolov:)</p> <p>Would you mind showing us your modified codes as well as your model classes?</p> <p>Thx again</p> 2011-10-06T01:04:28-04:004628132http://forums.asp.net/p/1726188/4628132.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>I must tell you that I was laid off 2 years ago and that I´m not in a good situation, financially speaking, now</p> <p>That said, I must also explain you that I come to a cyber coffee to do my research. I have cut off my Internet services, cable, phone and yesterday I was also cut my electrical supply</p> <p>Regarding what I´m doing to make a living is this: I´m trying to write a distributable invoicing application based on Dynamic Data. I started writing it almost a year ago. Haven´t finished yet because tehre are so many things regarding Dynamic data that are not really available for us anywhere else on the web but here and some blogs of some of your contributors</p> <p>So I'll be glad to submit what you've asked but I have to prepare it and upload it to you which means anoyher day that I must wait</p> <p>Having said all of that: I don´t understand what does my design have to do whit a simple question. All I need to know is this:</p> <p>I have a string &quot;Products&quot;. I also have other strings: names &quot;as simple text&quot; of the other NorthWind entities: &quot;Suppliers&quot;, &quot;Orders&quot;, etc.</p> <p>I also have the string of the name of the DataModel which is declared in the global.asax file</p> <p>All of them as strings</p> <p>I need a way of &quot;converting&quot; those strings in something which might be usable when I pass them as parameteres for calling a method</p> <p>I need a way of achieving &quot;instantiating&quot; those strings so I can have access to the properties of the entities they represent</p> <p>Like:</p> <p>var <strong>comingentity</strong> = table.Name;&nbsp;&nbsp;&nbsp; <br> (of which I got&nbsp;the string <em><strong>&quot;Products&quot;</strong></em>)</p> <p><br> [some kind of conversion here ??? ]&nbsp;&nbsp; <br> -- I don´t know what to do here to<strong><em> convert the string &quot;Products&quot; into the entity class Products</em></strong></p> <p><br> <strong>comingentity&nbsp;</strong>myentity = new <strong>comingentity</strong>(); <br> which must act like if I were writing <br> Products myproducts = new Products();</p> <p>and then I could use all of myproducts properties as If I had instantiated Products</p> <p>This is silly but if you have 100 entities you wouldn´t like to create switch statements for instantiating a 100 different entities, would you like that?</p> <p>So, as simple as that: How to convert a string which represents an entity name into the entity itself?</p> <p>&nbsp;</p> 2011-10-06T14:27:51-04:004628636http://forums.asp.net/p/1726188/4628636.aspx/1?Re+Reflection+About+Re: Reflection (About) <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Topolov</h4> I don´t know what to do here to<strong><em> convert the string &quot;Products&quot; into the entity class Products</em></strong></blockquote> <p></p> <p>Hello Topolov:)</p> <p>Don't worry and take it easy:)</p> <p>Since you are using EntityFramework and Products should be a public property of that... You can have&nbsp; a try like this:</p> <p>using (DbEntites dbe = new DbEntities)<br> {<br> &nbsp;&nbsp;&nbsp;&nbsp;dynamic&nbsp;products = &nbsp;dbe.GetType().<span style="text-decoration:underline"><strong>GetProperty(&quot;Products&quot;).</strong></span><span style="text-decoration:underline"><strong>GetConstructor(Type.EmptyTypes).Invoke(null);&nbsp;&nbsp;&nbsp;</strong></span> //Here you can use GetConstructor method and Invoke to generate a new products list. And then do what you like...<br> }</p> 2011-10-07T01:11:28-04:004633385http://forums.asp.net/p/1726188/4633385.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>Ok, late answer from my side buty as I said I'm currently cybering as to reach the net</p> <p>Not really lucky these days with money</p> <p>I'm obligued to &quot;unmark&quot; the non-answer answer from Mr. Dong but his code doesn't work as expected</p> <p>It doesn't work. <span style="text-decoration:underline"><strong></strong></span><span style="text-decoration:underline"><strong>GetConstructor is not available under taht type of construction</strong></span></p> <p>I've seen a previosu answer from him in which it works but then he uses Type at the begining of the line isntead of dbe whcih makes a huge difference in what are you doing and getting<span style="text-decoration:underline"><strong></strong></span></p> <p></p> 2011-10-11T00:27:14-04:004633397http://forums.asp.net/p/1726188/4633397.aspx/1?Re+Reflection+About+Re: Reflection (About) <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Topolov</h4> GetConstructor is not available under taht type of construction</blockquote> <p></p> <p>Sorry to hear that, but what's the matter with it?</p> <p>And You mean that you cannot use GetConstructor method?</p> <p>Or the intellisense doesn't recognize it?</p> <p>Thx again</p> 2011-10-11T00:58:03-04:004636283http://forums.asp.net/p/1726188/4636283.aspx/1?Re+Reflection+About+Re: Reflection (About) <p>Ok. Last time you said I've to calm down but no luck: yesterday a company (credit card debt) seized a good part of my assets</p> <p>I'm paying &#36;1 /hour for browsing the net at a cyber coffee</p> <p>Not good for me if I don't even have a &#36;1</p> <p>Having said that and after 3 days I come here to check answers to questions qhcih could ease my financial situation if I had my system developed ASAP as to start selling it</p> <p>Well, 3 days after and I see your anser ... I feel like crying</p> <p><strong>THE METHOD IS NOT THERE</strong>. As simple as that: <strong>GetConstructor</strong> doesn´t work because IT IS NOT THERE so please don't ask me and just try it in your computer</p> <p>Thanks anyway. I give up</p> <p>I'm working with 8 spreadsheets a to automate all my repetitive tasks because it seems that noone in the world knows or is willing to tell me how to make a string to be &quot;<strong>instantiatable</strong>&quot; at runtime which could as well be recognized as a type for the VS2010 intellisense as to achieve compiling it</p> <p>Is that so hard to do?</p> <p>OMG endless&nbsp; .... frustration .....</p> <p>I feel like crying when I have to write 100 classes, 100 events, 100 overrideings, etc jhust because VS200 does'0t allow you to pass strings as types</p> <p>OMG!!!!!!</p> 2011-10-12T18:28:41-04:00