wcf icon indicating copy to clipboard operation
wcf copied to clipboard

Fix for DataContractAttribute evaluation

Open BitKFu opened this issue 3 years ago • 5 comments

In my tests the DataContractAttribute could not be found, because Globals.TypeOfDataContractAttribute is a DataContractAttribute with ref. to the Framework Fork. Because this Framework Fork won't be used in "real live" projects, the DataContractAttribute could not be evaluated. To fix this, the code now searches for the same FullName and read the properties via reflection in order to build the DataContractAttribute used by the Framework Fork.

This fix will probably fix, because namespace will now be evaluated correctly #3812

BitKFu avatar Aug 02 '22 16:08 BitKFu

CLA assistant check
All CLA requirements met.

dnfadmin avatar Aug 02 '22 16:08 dnfadmin

I think you might be able to get a cleaner implementation using GetCustomAttributesData so you don't need to use reflection.

mconnew avatar Aug 04 '22 07:08 mconnew

@mconnew You're right, that's slightly nicer. Even if I think that internally the GetCustomAttributesData also uses reflection ;)

BitKFu avatar Aug 04 '22 15:08 BitKFu

It doesn't in quite the same way. There's a class called MetadataLoadContext which enables you to load assemblies in a non-executable way. You can even load assemblies of different versions than are currently loaded and being used in the process. GetCustomAttributes causes the attribute to be instantiated so doesn't work with MetadataLoadContext, but GetCustomAttributesData works fine as it doesn't instantiate an instance. The avoiding of reflection is because you can refer to properties and constructor arguments by name and avoid the reflection there. One thing to keep in mind is you have to check the constructor arguments as well as the properties as it doesn't run the constructor which would normally result in the property getting set.

mconnew avatar Aug 04 '22 16:08 mconnew

The DataContractAttribute has no constructor arguments. Please have a look at the code changes where I use the adviced GetCustomAttributesData method. It fixes the bug #3812 and I have no clue how the original implementation ever worked?!

BitKFu avatar Aug 05 '22 12:08 BitKFu

@imcarolwang can you please test this PR?

HongGit avatar Aug 19 '22 03:08 HongGit