简体   繁体   中英

Conversion of object array to Generic array typeof T Type at runtime C#

I am trying to convert a type of object to a type of T at run time, but I am having quite a time with it. The create method gets called and is supposed to perform some magic on the dictionary - providing the function with T[], the DbContext and the requested Type as System.Type - however I receive the following error from Visual Studio via intellisense and when I try to compile. Error Delegate 'System.Func' has some invalid arguments .

EDIT 2-16-2016

I am not understanding what the invalid arguments are - since I am passing in a DbContrext a System.Type and a generic T[] - if I remove the System.Type argument from the dictionary function and leave it as params T[] - passing in object works just fine! So my assumption is it has something to do with either passing the System.Type or some how the params statement - but I do not know - see the code fro my feeble attempts to convert and also see the bottom for a very small portion of my research effort.

Does any one know of a way I can use the dictionary function above - with out using a Generic Call for the Create Method in other words I can not pass in T[], nor can I pass it as Create ; it must be in the form as the method signature ( a variant is ok as long as no generics are used in the Method Signature).

The following code:

    private Dictionary<Operator, Func<T[], DbContext, System.Type, bool> > operators =
    new Dictionary<Operator, Func<T[], DbContext, System.Type, bool> >
    {   
    { Operator.Update, ( a , b, c ) => {// do work with abc} }

    } 


public bool Create(DbContext context, Operator op, System.Type requestedType, params object[] items )
{

    Type temp = requestedType.MakeArrayType();

    var elementsArray =  Activator.CreateInstance(temp);

    ArrayList elList = new ArrayList(items);
    for (int i = 0; i < elList.Count; i++)
    {
        Convert.ChangeType(elList[i], requestedType);
    }

    elementsArray = elList.ToArray();

    var listType = typeof(object);
    var constructedListType = listType.MakeGenericType(requestedType);

// Error Delegate 'System.Func<T[],System.Data.Entity.DbContext,System.Type,bool>' has some invalid arguments   

    return operators.ContainsKey(op) ? operators[op](items, context, requestedType) : false;

}

EDIT 2-16-2016 For the drive by downvoters - it takes very little effort to click down but it takes courtesy to comment - some links I have tried:

How to cast object array to generic type array

Casting Type array to Generic array?

how to upcast object array to another type of object array in C#?

I did find a solution but it took me a couple steps to solve the issue.

While I did know the error was in regards to my arguments being supplied [obviously - from the error message] , the fact that I passed in object without Syste.Type argument and it worked was throwing me off. After I added the additional argument 'C' of Type in the Dictionary function it quit working and threw this error - so as you can imagine I was thinking it was the System.Type argC. The code below shows what I did to get it to compile, it is not completely tested yet but it has compiled so I am one step further:

public bool Create(DbContext context, Operator op, System.Type requestedType, params object[] items)
{
    T[] itemsArray = (T[])new Object[items.Length];

    itemsArray = items.Select(eachObject => (T)eachObject).ToArray();

    return operators.ContainsKey(op) ? operators[op](itemsArray, context, requestedType) : false;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM