C
C#8mo 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
boiled goose
boiled goose8mo ago
if you have a wsdl you can directly generate a client with visual studio
uknys
uknys8mo 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
boiled goose
boiled goose8mo ago
so it's a useless wsdl 😐 how many types do you have to deserialize
uknys
uknys8mo 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"
boiled goose
boiled goose7mo 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
More Posts
Visual Studio vs Visual Studio Code?Which one should I use? I'm in Windows I have seen that Visual Studio is more complete as IDE, butHow are DTO utilizedSo I've been writing some end points for my learning project, and I stumbled on DTO. The concept I uFast search an object in a list using his attributeSo I have a list of ships and each ship has a length width, mass and a name. We need to write a methNeed authentication from MVC to WebAPIHi all, Can someone point me in the right direction here. I want to do something simple. I want to sVisual Studio doesnt show errors and projectsHey, I encountered a small issue with Visual Studio Community 2022 - it doesn't show projects in solAsp.Net Core How to create PDF Product Catalog With a template with an image in the backgroundI am developing a web application with Asp.net. The application will select the products on the e-coBest Way to store data for an Discord BotHey everyone, i need some input from some experts 😉 I am trying to build an Discord Bot who gives Rjust begun learning ASP.NET Core MVC and am looking for any good material to help mei am very new to ASP.Net so am just looking for some beginner friendly resources to begin properly lHello. I am facing an issue with null exception after calling OnPost method in my razor pages site.When i call method OnGet i load all of the information about property. public void OnGet(int✅ hey. im new to learning asp.net and was wondering if anyone had any suggestions as where to start?just any ideas of things to do/ things to watch would be great!