Instruct XmlSerializer to process serialized/deserialized data? - xml-serialization

I have an enum property. I want the serialized XML for this property to be the splitted camel-case string of the enum and vice versa.
I have two functions, one is ConcatCamelCase and the other is SplitCamelCase, I want the serializer to use them accordingly, is this possible by just decorating the field with an attribute?
If no, what are the other option without having to mess with all the other fields?

You'll have to do something like this:
public class SomeClass {
[XmlIgnore]
public MyEnum MyRealProperty {get;set;}
[XmlElement("MyRealProperty")]
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public string MyProxyProperty
{
get {return SplitCamelCase(MyRealProperty);}
set {MyRealProperty = ConcatCamelCase(value);}
}
}

You can explicitly set the name of everything that is serialized using the XMlSerialization attributes.
[XmlRoot("theNameYouWant")]
[XmlElement("theNameYouWant")]

Related

Microsoft Bond System.Object

I need to ensure the following class is serializable by Microsoft Bond. I am struggling to find a way to do this due to the inclusion of the object member.
public class BondRemotingRequestMessageBody : IServiceRemotingRequestMessageBody
{
public object Value;
public BondRemotingRequestMessageBody()
{
}
public BondRemotingRequestMessageBody(int parameterInfos)
{
}
public void SetParameter(int position, string paramName, object parameter)
{
Value = parameter;
}
public object GetParameter(int position, string paramName, Type paramType)
{
return Value;
}
}
Is there a way around this?
This is for an Azure Service Fabric ASR implementation.
Thanks in advance.
I don't know about the Service Fabric parts, but from a Bond perspective, all of the fields that are to be serialized must be known to Bond and must be a type (or convertible to a type) that can be serialized. In practice, this means that all of the data structures that you need to serialize is expressed in a collection of .bond files.
The closest to C#'s object would be a bonded<bond.Void> field that you later deserialize as the proper well-known derived type. You would need to include (or be able to infer/derive) the correct derived type. The polymorphic container example demonstrates this pattern; it uses an enum field in a base struct to carry the derived type information.

Entity Framework and implementation of IPrincipal/IIdentity

As far as I am aware, for the property to be saved in the database it cannot be ReadOnly.
IIdentity properties: AuthenticationType, IsAuthenticated and Name are all ReadOnly.
Is making the wrapper to the properties that need to be saved the only solution or there are better ones?
EDIT:
I might not have explained my question that well. Here is the sample code for one of the ReadOnly properties, I have added UserName property for the Entity Framework:
Public Property UserName As String
Get
Return _userName
End Get
Private Set(value As String)
userName = value
End Set
Public ReadOnly Property Name As String Implements System.Security.Principal.IIdentity.Name
Get
Return UserName
End Get
End Property
What I wanted to ask is if there is any better way of doing it.
IIdentity properties are read only but the implementation can have setters. If you are using EDMX for mapping you don't have to expose these setters as public.
Edit:
This is possible in C# so hopefully you can use similar approach with VB.NET (I can only read VB code, not write):
public interface ITest {
string Name { get; }
}
public class Test : ITest {
public string Name { get; set; }
}
The class offers setter even the interface didn't define it.
The EF persists objects, not interfaces. Your object can have whatever properties you would like it to have. You cannot add an interface to your entity model, but you can add an object type which implements that interface.

Custom sort in domaincollectionview

I'm using a DCV as a property in the View Model.
Everything works fine but what about custom sort?
Say I have a string property in my model which should be sorted alphanumerically.
How can I achieve such thing?
UPD:
Model:
public class MyModel
{
///...
public SomeProperty {get;set;}
}
xaml:
<data:DataTextColumn Binding={binding path=SomeProperty}, canusersort=true />
When sorting within the datagrid, the property gets sorted with disregard to alphanumeric order, i.e. in a regular string way. I'd like to apply my custom sort, e.g. by introducing my own IComparer. No API is available at least as I know of it.
Any clues?
The DomainCollectioView has special collection:
SortDescriptions
You could add next code in Your ViewModel:
DCV.SortDescriptions.Add(new SortDescription("SomeProperty ", ListSortDirection.Ascending));

Adding custom property to object returned from WCF RIA Services

I have a stored procedure in my Entity Framework Model. I've added a Function Import and mapped the results to a Complex Type.
I want to add an extra property to this Complex type, that I'll populate in my Domain Service, not coming back from the stored procedure. I added a myClass.shared.cs file and implemented added the property like so:
//myClass.shared.cs
public partial class myClass
{
public string myProperty {get;set;}
}
I populate this in my domain service when I return the object, e.g.:
public myClass GetMyClass(int myClassID)
{
myClass theClass= this.ObjectContext.StoredProc(myClassID).FirstOrDefault();
class.myProperty = 12345;
return theClass;
}
When I get the return values of this method on the client side theClass.myProperty is always null but all values from the stored procedure are populated, am I missing something?
I've tried decorating the myProperty with the [DataMember] attribute but this throws the error:
"The type 'myClass' already contains a
definition for 'myProperty'"
How can I get this to return the value set in the Domain Service to the client?
There was no need to put this in the shared.cs class. The shared.cs class copies the actual code over to the client side and is useful for adding methods etc. but to add a new property, all I had to do was add a partial class (NOT in myClass.shared.cs) and decorate it with DataMember.
public partial class myClass
{
[DataMember]
public string myProperty {get;set;}
}

Question about automatic properties

what happens if you implement an automatic property
public string Foobar { get; set; }
and then code the corresponding variable
private string foobar = string.Empty;
Will the automatic property use this variable or does the compiler generate
an additional variable?
No, the automatic property will not use your variable. It would be just like any other field called foobar.
The name smilarity does not influence the compiler in any way.
The compiler will generate a field behind the scenes but you do not have access to the backing field of the automatic property in any way.
This post shows how things work at the IL (Intermediate Langauge, Assembly of C#) level.
The compiler won't use that variable, no. To use your variable you will have to write
private string foobar = string.Empty;
public string Foobar
{
get { return foobar; }
set { foobar = value; }
}
If you have Resharper, you can set up templates to do this. Resharper will also generate a getter from an unused private variable for you.
Why would it? Backing field doesn't have to be (and often isn't) named this way.