tag:blogger.com,1999:blog-882821950355432699.post7175721260330021235..comments2022-12-07T21:25:53.386+11:00Comments on Paul Batum: A less fragile way to invoke a generic method with a dynamic type argumentPaulhttp://www.blogger.com/profile/18224234643439645641noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-882821950355432699.post-19674447725666841852008-09-03T20:07:00.000+10:002008-09-03T20:07:00.000+10:00Hey Mal,Which part don't you get? My post or the c...Hey Mal,<BR/><BR/>Which part don't you get? My post or the comments?<BR/><BR/>The mechanism I described still uses MakeGenericMethod. The point is that it doesn't use a string to identify the method (it uses an expression tree) so it will survive a refactoring.Paulhttps://www.blogger.com/profile/18224234643439645641noreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-41669516272121882182008-09-03T09:10:00.000+10:002008-09-03T09:10:00.000+10:00I'm not getting this at all. If you know the type ...I'm not getting this at all. If you know the type parameter at runtime why can't you just use Type.MakeGenericType?Malcolm Younghttps://www.blogger.com/profile/11060109670290978219noreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-4769206883300409752008-09-03T07:05:00.000+10:002008-09-03T07:05:00.000+10:00Well say for example you have this classpublic cla...Well say for example you have this class<BR/><BR/>public class Artist<BR/>{<BR/> private IList<Album> _albums;<BR/>}<BR/><BR/>And you want an ORM to instantiate one for you. This is illegal: <BR/><BR/>_albums = new List<object>;<BR/><BR/>Because contravariant generic type arguments are not supported. So what should it do?Paulhttps://www.blogger.com/profile/18224234643439645641noreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-60141313917344554242008-09-02T16:06:00.000+10:002008-09-02T16:06:00.000+10:00So what's the situation where this is actually...So what's the situation where this is actually needed/desirable?<BR/><BR/>e.g. If you want to create a List<type-not-known-at-compile-time> don't you just want a List<object> anyway? Like one of those damn ArrayList things?Andrew Moylanhttps://www.blogger.com/profile/15277166449214446604noreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-49799246072866539562008-08-25T12:00:00.000+10:002008-08-25T12:00:00.000+10:00The factory started out as generic but the problem...The factory started out as generic but the problem was with the method calls in LINQ. Based on the type, the SQL expression changes. i.e. DataContext.GetTable"User"().Where(u => u.UserID == x);<BR/><BR/>Thus, my interest in finding a way to downcast if I knew the type.<BR/><BR/>This is a bit of a sticking point right now and I feel like I'm trying to fit a square peg in a round hole. I might send Malcolm an email and ask if he's got any insight on how to tackle this problem or if I'm just trying to complicate things.<BR/><BR/>This thread has made me realise that I'm banging my head on a wall. Cheers Paul.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-55542008419717490992008-08-23T00:24:00.000+10:002008-08-23T00:24:00.000+10:00Err.. whoops. I mean C# is statically typed. Early...Err.. whoops. I mean C# is statically typed. Early/late binding is something altogether different.Paulhttps://www.blogger.com/profile/18224234643439645641noreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-52179681623413970482008-08-23T00:19:00.000+10:002008-08-23T00:19:00.000+10:00I'm having trouble making sense of what you ar...I'm having trouble making sense of what you are after, because it sounds like you are trying to CAST to a dynamic type. As in:<BR/><BR/>Type theType = GetSomeTypeAtRuntime();<BR/>object newInstance = Activator.CreateInstance(theType, null, null);<BR/><BR/>??? entity = (Entity<theType>)newInstance;<BR/><BR/>The last line doesn't compile for the same reason that my example with the list of strings doesn't. But there is another problem: what the hell would you replace those questions marks with? As far as I can tell, there is nothing that you CAN put there. C# is an early bound language.<BR/><BR/>Now if your factory method is generic, then you are obviously fine. Why isn't your factory method generic?Paulhttps://www.blogger.com/profile/18224234643439645641noreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-43729104672728577802008-08-22T13:51:00.000+10:002008-08-22T13:51:00.000+10:00My generic class just has a default constructor (n...My generic class just has a default constructor (no parameters). It literally looks something like:<BR/><BR/>public class Foo : BaseClass"T"<BR/><BR/>In my factory class I have some code which does:<BR/><BR/>object theClass = Activator.CreateInstance(theType, null, null);<BR/><BR/>If I can discover the type of T, is there a way to cast it from an object to BaseClass"T"?<BR/><BR/>I apologise for my mistake in saying "upcast". In this instance, it's more of downcasting it to the correct type.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-42783515910065711952008-08-21T18:04:00.000+10:002008-08-21T18:04:00.000+10:00Hey there Alvin,Instantiation is a slightly differ...Hey there Alvin,<BR/><BR/>Instantiation is a slightly different story. What constructors are defined on your generic class?<BR/><BR/>I'm not sure I follow what you mean by "upcast it to the right type". Can you expand on this a bit?Paulhttps://www.blogger.com/profile/18224234643439645641noreply@blogger.comtag:blogger.com,1999:blog-882821950355432699.post-29911760756513643772008-08-21T11:43:00.000+10:002008-08-21T11:43:00.000+10:00Hi Paul,I found this post quite informative and co...Hi Paul,<BR/><BR/>I found this post quite informative and coincidentally extremely useful with what I'm doing right now.<BR/><BR/>The answer to my question might seem obvious but I've had quite a hard time struggling with it.<BR/><BR/>Basically I want to use reflection to instantiate a generic class. I've got something that looks like this:<BR/><BR/>public class InternalClass"T" Where T: IEntity, class<BR/>{<BR/>}<BR/><BR/>Please excuse the "". The parser doesn't like angled brackets.<BR/><BR/>I already know what T is and I can use reflection to create an object of that type but I cannot for the life of me find out how to upcast it to the right type.<BR/><BR/>Your post gave me a few ideas I might try later but I'm keen to see your response for this.<BR/><BR/>Cheers<BR/>AlvinAnonymousnoreply@blogger.com