1

Is it possible to build a type name and recast an object to a specific type? The target types will be contained in the same assembly, and there will be hundreds. Example code:

var isPropertyInfo = objCollection as PropertyInfo;
if (isPropertyInfo != null)
{
    var propType = ((PropertyInfo)objCollection).PropertyType;
    var containerType = StripContainerType(propType.FullName);
    var itemType = StripItemType(propType.FullName);
    var fullTypeName = containerType + "<" + itemType + ">";
        // ex:  "System.Collections.Generic.ICollection<Customer>"
    var fullType = Type.GetType(itemType);
    dynamic coll;
    // now I want to do something *LIKE* (pseudocode-ish)
    // coll = objCollection as fullType;

Do not consider the last line as valid code. It's just where I'm stuck.

Robert Kerr
  • 1,291
  • 2
  • 17
  • 34
  • 3
    Casting is a compile time issue. If you're building up the string in your program, you can't possibly also have the compiler know what the result is. Do you mean *convert* instead of cast? – Asad Saeeduddin Aug 31 '15 at 21:02
  • There are definitely a number of duplicate .. – user2864740 Aug 31 '15 at 21:05
  • @Asad Possibly but it feels like I want to cast, because the underlying object has never changed types. I started with an ICollection, passed it to this method as an object, and now want to enumerate its members and take action. But to do that, I first have to get them back to the ICollection, in this example. – Robert Kerr Aug 31 '15 at 21:06
  • @user2864740 great, I've been searching for an exact parallel to this question, and have not found it. please link and thanks. – Robert Kerr Aug 31 '15 at 21:06
  • This seems like a job for Reflection. – Ian McLaird Aug 31 '15 at 21:07
  • @RobertKerr Search for the error message the compiler is telling you. Also make sure to *include* such messages in questions (not necessarily in the title). In any case this cast is useless because the target expression is `dynamic` - and not whatever type is represented by `fullType`. Now, you could check the runtime type of the `objCollection` value.. – user2864740 Aug 31 '15 at 21:08
  • @user2864740 user I did say in the original message the last line is not valid. It's where I'm stuck. Consider the last token pseudocode filler. Hope that helps. Also if you're going to mark a question for closure as duplicate, it's helpful if you link to the duplicates. I have not found one. Thanks. – Robert Kerr Aug 31 '15 at 21:11
  • @RobertKerr "Last line is not valid" is *not* what the compiler reported - go ahead and *try* compiling with it in! If searching for the error message you will find a duplicate (even before sprinkling in keywords like 'dynamic'), I promise. Have a good day with vagueness. – user2864740 Aug 31 '15 at 21:11
  • @user2864740 updated for his highness. – Robert Kerr Aug 31 '15 at 21:14
  • @RobertKerr As I said, the compiler has no idea what the result of your string manipulation is. The only way to find out is to run that code, and in order to run the code you need to compile it, so you're stuck in a bit of a chicken and egg situation. The typical solution to this kind of problem is to either a) throw in the towel and accept dynamic typing, or b) do some metaprogramming, which .NET is getting better for, but is still not great at. – Asad Saeeduddin Aug 31 '15 at 21:14
  • @RobertKerr If you want to get it to an `ICollection`, and you *know* the thing is an `ICollection`, why can't you just do a direct cast? – Asad Saeeduddin Aug 31 '15 at 21:16
  • @Asad thanks, guess it's time for deeper dive into CodeDom possibly, might be able to do something in that direction. The types are already compiled into the assembly, I'm not building them at runtime. I'm just taking a type which is already in a collection, passing it as object, then trying to get it back as that strongly typed collection, using the metadata that's been carried along with it. – Robert Kerr Aug 31 '15 at 21:17
  • @RobertKerr Could you code up a toy example of what you're describing? There's almost certainly a better way to do it (e.g. with dynamic dispatch) than what you're embarking on. – Asad Saeeduddin Aug 31 '15 at 21:19
  • 1
    Also, are you familiar with generics? – Asad Saeeduddin Aug 31 '15 at 21:21
  • @Asad yes I am and yes I can. This is a minor part of this other question, ( http://stackoverflow.com/questions/32234584/serialization-of-hierarchy-into-folders-and-files )which someone said was too vague, so I posted this specific part, which is now closed, *sigh*, while the linked question is NOT the same. Also, as I point out in the question, the assembly contains hundreds of types. I am trying to write a method to avoid having to hard-code every type, now and in the future. – Robert Kerr Aug 31 '15 at 21:23
  • 1
    @RobertKerr So the part I'm not understanding is, if you want to cast to `ICollection`, why don't you just do that? What is the point in building this string? Some example code here would help clarify – Asad Saeeduddin Aug 31 '15 at 21:25
  • the assembly contains hundreds of types. I am trying to write a method to avoid having to hard-code every type, now and in the future. Look at http://stackoverflow.com/questions/32234584/serialization-of-hierarchy-into-folders-and-files the two methods BuildInstanceFolder and BuildCollectionFolder, to see the context. – Robert Kerr Aug 31 '15 at 21:26
  • 1
    What is the input and output of this method? Does the caller know what the desired output type is? If so, you should provide that using generic type parameters, then cast to it. – Asad Saeeduddin Aug 31 '15 at 21:27
  • 1
    @RobertKerr Just saw your edit. So would something like `BuildCollectionFolder(string parentInstanceFolder, ICollection objCollection)` not work? – Asad Saeeduddin Aug 31 '15 at 21:31

0 Answers0