In other words, in VB, I have to turn off the type checker for the entire module that contains the line. With Option Strict On, this line upsets the compiler, as expected. With VB, the line goes like this: Dim listResult = tgtObj.GetType().GetMethod("GetSomeData").Invoke(tgtObj, Nothing) Then, when the dynamic object gets passed to the Export method, the DLR (Dynamic Language Runtime) looks to see if it can implicitly cast the object to meet the requirements of the method signature. Since dynamic bypasses static type checking at compile time, we don't get a compile error. The fix is to change var to dynamic: dynamic listResult = tgtObj.GetType().GetMethod("GetSomeData").Invoke(tgtObj, null) Now, you can create a new List, assign (List) listResult to it, but that defeats the purpose of dynamic invocation in the first place. The problem is that the compiler can't find the IList metadata, because it's looking at the System.Object interface. No thanks!! That's what I'm trying to avoid here. Try specifying the type arguments explicitly. The type arguments for method '.Export.' cannot be inferred from the usage. If I try to execute the Export method, thus: myExportObj.Export(listResult, parms.filePath, parms.fileType) Of course, a List is also a System.Object, but the interface exposed is the System.Object interface, not the IList interface. Invoke returns an object of type System.Object. Next, I'm sending the listResult object to a generic method called Export, which looks like this: void Export(List exportList, string filePath, byte fileType) where T: class The signature of "GetSomeData" is: public List GetSomeData()Įach one of the methods invoked returns some sort of List object. Which method to invoke here is dependent on a string parameter passed into this object at runtime, so, value of "GetSomeData" varies at runtime. The reason I'm doing this is that "GetSomeData" could be any of a number of methods, each getting different data. I have this piece of code in C#, that uses reflection to dynamically invoke a method at runtime: var listResult = tgtObj.GetType().GetMethod("GetSomeData").Invoke(tgtObj, null) This will demonstrate what Basic is saying about VB not having the same granularity in this as C#.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |