geostocker
geostocker

Reputation: 1200

Converting a collection to an object[]

I've been looking for a way to move the items of a collection (I haven't decided on what sort of collection) to an Object[]. I'm attempting to invoke a method through reflection using a the calling method's parameter (the collection) as the invoked method's parameters.

I have a couple (UPDATE) of methods that I wish to access through a REST api that in turn requires different amounts of parameters. As such I've decided to use a collection as one of the parameters that the REST api's Update requires. But since I need to invoke a method from reflection I also need to somehow parse / cast the collection to an Object[] somehow.

This is what I have right now, and as mentioned I seem to be a bit stuck...

Also, please keep in mind that this is not a question on how to set up the REST service (like my other), but simply how to best cast the collection to the correct invoke parameters. Additionally any help or advice on what sort of collection to use would be greatly appreciated.

public void Update(String proc, List<String> parameters)
{
    var myType = typeof(JaberoDC.JaberoDC.JaberoDC);
    var list = parameters;
    var method = myType
        .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
        .Single(mi => string.Equals(mi.Name, proc, StringComparison.OrdinalIgnoreCase));
    var subject = Activator.CreateInstance(myType);
    var result = method.Invoke(subject, list);
}

Here are two examples of methods that I intend to invoke:

public bool UPDATEActivities(int? iID, int? iWorksiteID, string strActivityName, string strMethodOfWork, DateTime? dtPlannedStart, DateTime? dtActualStart, DateTime? dtPlannedFinish, DateTime? dtActualFinish, bool? blMileStoneFlag, bool? blActivityCutShort, int? iInterruptionMinutes, string strVarianceReason, string strConn, string strUserName);

public bool UPDATEWorksiteEntry(int? iID, string strJobName, string strJobID, string strSiteName, int? iCalendarWeek, int? iMainContractor, string strJobStartFrom, string strJobEndAt, string strSACStaffAssigned, string strReferenceNumber, int? iTerritory, string strFunction, string strItemNumber, string strLine, int? iWorksiteType, string strUID, string strEventNumber, string strRestrictions, string strLatitude, string strLongitude, string strPlannedWork, int? iPlannedStartMileage, int? iPlannedFinishMileage, DateTime? dtPlannedStart, DateTime? dtActualstart, DateTime? dtPlannedFinish, DateTime? dtActualFinish, int? iActualFinishMileage, int? iActualFinishYardage, int? iActualStartMileage, int? iActualStartYardage, int? iPlannedFinishYardage, int? iPlannedStartYardage, string strSACDayPhoneNo, string strSACNightPhoneNo, string strELR, string strSacPoint, bool? blTunnelSignIn, string strConn, string strUserName);

Upvotes: 0

Views: 308

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

Assuming you need object[] as for list of arguments for Invoke - calling ToArray on IEnumerable<object> is one option*:

   List<string> parameters = new List<string>{"a","b"};
   var paramsAsObjects = parameters
        .Cast<object>() // need to cast each item from string to object
        .ToArray();

   methodWithTwoStringParam.Invoke(..., paramsAsObjects);

*Note: Invoke requires types of items in arrays to match types of parameters. Casting to object does not change type of parameter. If methods you need to call take different types as arguments (i.e. not all string) than you'd need to stick with passing arguments to your helper as collection of base types of all possible parameters. Most likely you endup with collection of object elements - like List<object> or just avoid all conversions and construct object[] to start with.

If your values start from untyped strings (i.e. user input) you may consider using Convert.ChangeType instead of Cast to try to match parameter types. ChangeType works for string to numeric types conversions, if you need more complicated types - use your own conversion logic.

Upvotes: 1

Related Questions