issue converting generics list to array and back  
Author Message
Joey Bradshaw





PostPosted: Visual C# Language, issue converting generics list to array and back Top

I'm new to generics and they are being a real pain when it comes to my web services. I've got a winforms app that uses a proxy web class which invokes methods on a web service. The web method "RemoveRole" takes a parameter of List<Guid>. It throws up saying it could not convert object[] to List<Guid> so I'm trying to figure out how to put the plumbing in place to make the conversion happen. Currently the code below throws "Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'object[]".

1. How do i get the following to work

and..

2. What plumbing do I need in the web service code to convert from object[] back to List<Guid>

public void RemoveRole(List<Guid> roles)
{
object[] parameters = roles.ConvertAll(new Converter<Guid, object>(GuidToObject));
try
{
results = this.Invoke("RemoveRole", parameters);
}
catch (SoapException e)
{
throw e;
}
}

private static object GuidToObject(Guid n)
{
return (object)n;
}



Visual C#6  
 
 
Joey Bradshaw





PostPosted: Visual C# Language, issue converting generics list to array and back Top

I think i found the answer to problem #1 which is:

object[] parameters = Array.ConvertAll(roles.ToArray(), new Converter<Guid, object>(GuidToObject));

Just need to figure out prob #2 now. With this new code in place the web service throws the error, "{"Unable to cast object of type 'System.Guid' to type 'System.Collections.Generic.List`1[System.Guid]'."}"


 
 
Joey Bradshaw





PostPosted: Visual C# Language, issue converting generics list to array and back Top

Ok, so it was all a misunderstanding of how the params are used on the Invoke method. By simply assigning the entire generics list to the first index of the object[] everything worked out! BTW just another case of don't believe everything you read and persistance does pay off! See the section titled "What Generics Cannot Do" http://www.hide-link.com/

Proxy class:

public void RemoveRole(List<Guid> roles)

{

object[] results = null;

object[] parameters = new object[1];

parameters[0] = roles;

try

{

results = this.Invoke("RemoveRole", parameters);

}

catch (SoapException e)

{

throw e;

}

}

Web service:

[WebMethod]

[System.Diagnostics.De****StepThroughAttribute()]

public void RemoveRole(List<Guid> roles)

{

//Do something…

}