mercredi 13 juillet 2016

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);





Aucun commentaire:

Enregistrer un commentaire