Click here to Skip to main content
15,868,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Could you please suggest

I have TYPE of list of an elements of "unknown" type ... say, the Type = typeof(List<of_something>)

how to determine whether it's a type of List<> or Array
how to determine the type of an elements included - that is typeof(of_something)

thank you forward for advice

What I have tried:

I don't have an idea how to implement this
I am stuck
Posted
Updated 9-Aug-18 23:46pm

Yes, you can do it.
Try these extension methods:
C#
public static class Extensions
    {
    public static Type GetContainedType<T>(this List<T> ls)
        {
        return typeof(T);
        }
    public static Type GetContainedType(this Array ar)
        {
        return ar.GetType().GetElementType();
        }
    }
You can then call them on your collections:
C#
List<string> sl = new List<string>();
string[] sa = new string[10];
List<int> il = new List<int>();
int[] ia = new int[10];
Type tsl = sl.GetContainedType();
Type tsa = sa.GetContainedType();
Type til = il.GetContainedType();
Type tia = ia.GetContainedType();
 
Share this answer
 
Comments
[no name] 10-Aug-18 5:46am    
Hi! Thank you for your answer!
Although, this is not exactly what I meant

Please see my version underneath
Hi!

I supposed something like that:

C#
class Program
{
    static void Main(string[] args)
    {
        Type t = (new List<MyType>() {new MyType(), new MyType(), new MyType() }).GetType();    //this is just for pupose of getting TYPE of List<string>
        //at this position we are interested in Type, not object itself

        //there is no object - just "Type" or PropertyInfo

        TypeInfo pi = t.GetTypeInfo();
        PropertyInfo prop = pi.DeclaredProperties.Where(c => c.Name == "Item").FirstOrDefault();
        Type tp = prop.PropertyType;
        object obj = tp.InvokeMember("", BindingFlags.CreateInstance, null, null, null);
        //now I need to cast the obj to the appropriate type to call the method Name()
    }
}

class MyType
{
    public string Name()
    {
        return "It's me";
    }
}


now I need to cast the obj to the appropriate type to call the method Name()
 
Share this answer
 
Comments
Nelek 10-Aug-18 8:25am    
Please use the "improve question" if you need to add things to the question. Posting solutions will only make it more difficult to help you because they are not chronologically ordered
C#
class Program
    {
        static void Main(string[] args)
        {
            Type t = (new List<MyType>() {new MyType("1"), new MyType("2"), new MyType("3") }).GetType();    //this is just for pupose of getting TYPE of List<string>
            //at this position we are interested in Type, not object itself
            //this is no object - just "Type" or PropertyInfo
            TypeInfo pi = t.GetTypeInfo();
            PropertyInfo prop = pi.DeclaredProperties.Where(c => c.Name == "Item").FirstOrDefault(); //no matter how much objects were built and included in List (again - there is no object - just TYPE)
            Type tp = prop.PropertyType;
            object obj = tp.InvokeMember("", BindingFlags.CreateInstance, null, null, new object[] { "from Invoke" });

            //now we need to cast the obj to the appropriate type to call the method Name()

            object name = tp.InvokeMember("Name", BindingFlags.InvokeMethod, null, obj, null);
            //on my opinion there is just one way to invoke inner method without casting an object "obj" to the type "tp"
            //but still it would be better to obtain an object "obj" of properly type.
        }
    }

    class MyType
    {
        public MyType()
        {
            Console.Write("I was born => my name is: ");
        }

        public MyType(string s) : this()
        {
            Console.WriteLine(s);
        }

        public string Name()
        {
            return "It's me " ;
        }
    }


on my opinion there is just one way to invoke inner method without casting an object "obj" to the type "tp"
but still it would be better to obtain an object "obj" of properly type.
 
Share this answer
 
Comments
Nelek 10-Aug-18 8:25am    
Please use the "improve question" if you need to add things to the question. Posting solutions will only make it more difficult to help you because they are not chronologically ordered
[no name] 10-Aug-18 8:50am    
I can see the button "improve" on my own implementations
Could you please suggest, how to answer the participants

Thank you
Nelek 10-Aug-18 9:52am    
using the "have a quetion or comment" or the "reply" to another comment (as I am now doing with this message)
[no name] 11-Aug-18 12:18pm    
unfortunately it doesn't allow to send a code in the reply - simple text only
Nelek 11-Aug-18 17:36pm    
That's why I told you to update your question, you can write "adition after the comment of XXXX" and then your code

This way, everyone reading your question will see all the relevant information at once, without having to read all answers and comments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900