C
C#12mo ago
uknys

Generic XML Node to Class

Hello, I need to interface with WSUS (using dotnet 8) using ApiRemoting (SOAP), in some functions, it returns a List<Object> (it's intended afaik in the WSDL file). Is there a way to deserialize the generic type to a specific one without resorting to manual casting and enumerating one by one the list ? (with reflection for exemple) exemple from the wsdl : https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-wsusar/c162f3b0-61bb-4134-815c-f089962f2e30 Thanks in advance ! 🙂
[MS-WSUSAR]: Search for a Client Computer
This example demonstrates how to search for a client computer named WSUSAR-CL1.example.com by using the ExecuteSPSearchComputers (section
5 Replies
Omnissiah
Omnissiah12mo ago
if you have a wsdl you can directly generate a client with visual studio
uknys
uknysOP12mo ago
It's already generated using dotnet-svcutil, but still doesn't help the GenericReadableRow (which in the end is a List<Object>) which I need to manually unmarshall sadly
Omnissiah
Omnissiah12mo ago
so it's a useless wsdl 😐 how many types do you have to deserialize
uknys
uknysOP12mo ago
around 10 found a way :
c#
internal static class MyExtensions
{
internal static T Cast<T>(this GenericReadableRow r) where T : new()
{
var result = new T();

foreach (var prop in typeof(T).GetProperties())
{
if (prop.GetCustomAttribute<ValueAttribute>() is ValueAttribute attr)
{
prop.SetValue(result, r.Values[attr.Index]);
}
}

return result;
}
}
c#
internal static class MyExtensions
{
internal static T Cast<T>(this GenericReadableRow r) where T : new()
{
var result = new T();

foreach (var prop in typeof(T).GetProperties())
{
if (prop.GetCustomAttribute<ValueAttribute>() is ValueAttribute attr)
{
prop.SetValue(result, r.Values[attr.Index]);
}
}

return result;
}
}
I have still a GenericReadableRow, but I can cast it dynamicaly using attributes to make it "generic"
Omnissiah
Omnissiah12mo ago
i would move the logic into a mapper class or inside a static Create method of the type itself so that there is more error checking and explicitness
Want results from more Discord servers?
Add your server