How to do dynamic downcasting in vb.net?

I have several classes that all come from SuperClass.

When classes are created, they are all placed in a list (from SuperClass). When I scroll through the list, I would like to hide the SuperClass object before its baseObject and put it in the correct list. (I have one list created for each of the SuperClass subtypes).

You can determine:

 If TypeOf SuperClass Is SubClass Then
      listOfSubClass.Add(DirectCast(SuperCLass, SubClass)
 End If

but it’s a lot of work when there are several classes.

Using

SuperClass.GetType.FullName

I get a subClass type.

My question is: can this be used to dynamically migrate a SuperClass object? in psudoCode:

For each SuperClass
     Dim temp As SuperClass.GetType.FullName = _
                       DirectCast(SuperCLass, SuperClass.GetType.FullName 
     list.add(temp)
Next

Edit: I was hoping I could use a variable instead of creating one case for each SubClass and do it all in one cycle.

+3
1

LINQ OfType? , . :

list.AddRange(superList.OfType(Of SubClass)())

:

list = superList.OfType(Of SubClass)().ToList()

, , , VB.NET


: , :

namespace Demo.ListFilter
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;

    class Program
    {
        private class SuperClass
        {
        }

        private class SubClassA :
            SuperClass
        {
        }

        private class SubClassB :
            SuperClass
        {
        }

        static void Main(string[] args)
        {
            var superList = new List<SuperClass>()
            {
                new SuperClass(),
                new SuperClass(),
                new SuperClass(),
                new SuperClass(),
                new SubClassA(),
                new SubClassA(),
                new SubClassA(),
                new SubClassB(),
                new SubClassB(),
                new SubClassB(),
                new SubClassB()
            };

            var listA = new List<SubClassA>();
            var listB = new List<SubClassB>();

            SplitList(superList, listA, listB);

            Console.WriteLine("List A: {0}", listA.Count);
            Console.WriteLine("List B: {0}", listB.Count);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        static void SplitList(IList superList, params IList[] subLists)
        {
            foreach(var subList in subLists)
            {
                var type = subList.GetType().GetGenericArguments()[0];
                FilterList(superList, subList, type);
            }
        }

        static void FilterList(IList superList, IList subList, Type type)
        {
            var ofTypeMethod = typeof(Enumerable).GetMethod("OfType");
            var genericMethod = ofTypeMethod.MakeGenericMethod(type);
            var enumerable = (IEnumerable)genericMethod.Invoke(null, new[] { superList });

            foreach(var item in enumerable)
            {
                subList.Add(item);
            }
        }
    }
}

: :

    static void SplitList(IList superList, params IList[] subLists)
    {
        var ofTypeMethod = typeof(Enumerable).GetMethod("OfType");

        foreach(var subList in subLists)
        {
            var subListType = subList.GetType();
            var type = subListType.GetGenericArguments()[0];
            var genericOfTypeMethod = ofTypeMethod.MakeGenericMethod(type);
            var enumerable = genericOfTypeMethod.Invoke(null, new[] { superList });

            var addRangeMethod = subListType.GetMethod("AddRange");
            addRangeMethod.Invoke(subList, new[] { enumerable });
        }
    }

!

+4

Source: https://habr.com/ru/post/1734515/


All Articles