We convered a bunch of wdsl files to C# and these files cannot be altered at all. Inside of the C# files each with a unique namespace (there are 1000 of them), they all have a RequestHeader class. Note that these classes are exactly the same for each namespace – but they are technically different because they all reside inside of a different namespace. We cannot delete these classes and move them to a common area due to how many there are and constraints with our vendor.
I want to be able to perform common operations on the RequestHeader in our API project, with a RequestUtil file. But because they are technically different because they reside in different namespaces, it causes type conversion errors. What is the solution? Use dynamic types? I want to be able to call a function like:
public static void (dynamic header, string config){
header.config = config;
// Below we have to instantiate a type that sits inside of the header class, but because the namespaces are different, idk what to do! Please help.
header.UniqueType = new UniqueType(); // ERROR! Cannot instantiate this type because we don't have the namespace. Should we use something like Activor.CreateInstance?
}
Please let me know what the solution might be. I think it is going to revolve around using dynamic types and Activator.CreateInstance for unique nested types that need to be instantiated. Thanks!
3
Yes, it does sound like dynamic
is a good tool for this use case, but you probably don’t want to let dynamic
code spread throughout your code base. I’d create an interface that represents the API you expect to be common to all the RequestHeader classes. Then I’d create an Adapter class that implements this interface by invoking a dynamic
injected object.
1