I have a base class with a few properties:
// must include any derived classes here as known types or else they will throw errors on serialization
[KnownType(typeof(CollaborationEventMeasureDistance))]
[DataContract]
public partial class CollaborationEvent
{
public bool HasBeenTransported { get; set; }
public Guid MessageBoxGuid { get; set; }
public CollaborationEvent()
{
HasBeenTransported = false;
}
}
And a derived class with some properties of its own:
public class CollaborationEventMeasureDistance : CollaborationEvent
{
public Geometry Geometry { get; set; }
}
When I serialize the derived class, all of its properties are serialized, but the properties it inherits from the base class are not:
<CollaborationEvent i:type="CollaborationEventMeasureDistance">
<Geometry xmlns:d4p1="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry"
i:type="d4p1:Polyline">
<d4p1:spatialReference>
<d4p1:wkid>26910</d4p1:wkid>
</d4p1:spatialReference>
<d4p1:paths>
<d4p1:points>
<d4p1:point>
<d4p1:spatialReference>
<d4p1:wkid>26910</d4p1:wkid>
</d4p1:spatialReference>
<d4p1:x>460892.23924271885</d4p1:x>
<d4p1:y>5367682.5572773879</d4p1:y>
</d4p1:point>
<d4p1:point>
<d4p1:spatialReference i:nil="true" />
<d4p1:x>461001.35841108358</d4p1:x>
<d4p1:y>5367648.5755294543</d4p1:y>
</d4p1:point>
</d4p1:points>
</d4p1:paths>
</Geometry>
</CollaborationEvent>
Can anyone point out what I am doing wrong?
I expect my XML to look more like:
<CollaborationEvent i:type="CollaborationEventMeasureDistance">
<HasBeenTransported>True</HasBeenTransported>
<MessageBoxGuid>blah</MessageBoxGuid>
<Geometry xmlns:d4p1="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry"
i:type="d4p1:Polyline">
<d4p1:spatialReference>
<d4p1:wkid>26910</d4p1:wkid>
</d4p1:spatialReference>
<d4p1:paths>
<d4p1:points>
<d4p1:point>
<d4p1:spatialReference>
<d4p1:wkid>26910</d4p1:wkid>
</d4p1:spatialReference>
<d4p1:x>460892.23924271885</d4p1:x>
<d4p1:y>5367682.5572773879</d4p1:y>
</d4p1:point>
<d4p1:point>
<d4p1:spatialReference i:nil="true" />
<d4p1:x>461001.35841108358</d4p1:x>
<d4p1:y>5367648.5755294543</d4p1:y>
</d4p1:point>
</d4p1:points>
</d4p1:paths>
</Geometry>
</CollaborationEvent>
Thanks
Assuming your Geometry class is serializable, try something like this:
[DataContract, Serializable]
public class CollaborationEventMeasureDistance : CollaborationEvent
{
[DataMember]
public Geometry Geometry { get; set; }
}
[KnownType(typeof(CollaborationEventMeasureDistance))]
[DataContract, Serializable]
public partial class CollaborationEvent
{
[DataMember]
public bool HasBeenTransported { get; set; }
[DataMember]
public Guid MessageBoxGuid { get; set; }
public CollaborationEvent()
{
HasBeenTransported = false;
}
}
Related
In a separate project/solution I have a class named CompleteField that is referenced in multiple WCF projects/solutions.
The WCF generates a partial class from CompleteField which is not desired.
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
[System.Runtime.Serialization.DataContractAttribute(Name="CompleteField", Namespace="http://schemas.datacontract.org/2004/07/Common.Web.Models")]
public partial class CompleteField : object
{
//...
}
Using the attribute KnowType I thought I would be able to omit this behavior (since I have done this in the past).
Below my CompleteField class
[DataContract]
[KnownType(typeof(ValidatorType))]
[KnownType(typeof(ObjectType))]
[KnownType(typeof(ICompleteField))]
public class CompleteField : ICompleteField
{
[DataMember]
public string Name { get; set; }
[DataMember]
public ObjectType Type { get; set; }
}
public interface ICompleteField
{
string Name { get; set; }
ObjectType Type { get; set; }
}
[DataContract]
public enum ValidatorType
{
[EnumMember]
//...
}
[DataContract]
public enum ObjectType
{
[EnumMember]
//...
}
public CompleteField(ObjectType type)
{
this.Type = type;
}
}
Does anyone know why the WCF still generates the partial class this?
The partial class is generated because I used a Constructor in CompleteField.
I am using protobuf-net version 2.3.2.0.
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
[ProtoInclude(12, typeof(SubClass))]
public class BaseClass
{
public string Id { get; set; }
public string Name { get; set; }
}
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class SubClass : BaseClass
{
public string PropertySub { get; set; }
}
As my base class is inherited from many child classes.
How can I avoid placing attribute [ProtoInclude] on my base classes so that props in my base class deserialize as expected.
Say I have a class like...
public abstract class Base
{
public abstract IAttributes Attributes{ get; set; }
}
public interface IAttributes
{
string GlobalId { get; set; }
}
And a class like this...
public class ImplementAttributes : IAttributes
{
public string GlobalId { get; set; } = "";
public string LocalId { get; set; } = "";
// Other Properties and Methods....
}
And then I implement it like...
public class Derived: Base
{
public new ImplementAttributes Attributes { get; set; }
}
Now, I realise the above will not work because I can't override the property Attributes and if I hide it with new then the following bellow is null because the Base property does not get written.
public void DoSomethingWithAttributes(Base base)
{
var Foo = FindFoo(base.Attributes.GlobalId); // Null because its hidden
}
But I would like to be able to access the Base and Derived property attributes eventually like Above.
Can this be accomplished? Is there a better way?
You can use generics:
public abstract class Base<T> where T: IAttributes
{
public abstract T Attributes{ get; set; }
}
public interface IAttributes
{
string GlobalId { get; set; }
}
And
public class Derived: Base<ImplementAttributes>
{
public override ImplementAttributes Attributes { get; set; }
}
And then:
public void DoSomethingWithAttributes<T>(Base<T> b) where T : IAttributes
{
var Foo = FindFoo(b.Attributes.GlobalId);
}
You can pass Derived instances without specifying a type parameter explicitly:
Derived d = new Derived();
DoSomethingWithAttributes(d);
I created the following abstract class:
public abstract class AbstractClass
{
public abstract string Name { get; set; }
public abstract object Value { get; set; }
}
Now I want to derive two classes of the abstract class. I want to use an enum instead of the type object. My derived classes look like this:
First class:
public class InheritanceClass1:AbstractClass
{
public override string Name { get; set; }
public override FirstEnum Value { get; set; }
}
Second class:
public class InheritanceClass2 : AbstractClass
{
public override string Name { get; set; }
public override SecondEnum Value { get; set; }
}
I'm getting an error showed in my code, that the type of the property Value isn't object. I tryed to use the new-keyword instead of override like this:
In my abstract class:
public object Value { get; set; }
In my derived class:
public new FirstEnum Value { get; set; }
But if I create a List<AbstractClass> I have the problem that I can't use it for example for Linq because I would retrieve the "wrong" property. It is just hided, but still there, so I have to override the property.
So how do I have to change my abstract class and my derived classes, that I can use different types in my derived classes?
You can use abstract class like this:
public abstract class AbstractClass<T>
{
public abstract string Name { get; set; }
public abstract T Value { get; set; }
}
And derived class will change like this:
public class InheritanceClass1 : AbstractClass<FirstEnum>
{
public override string Name { get; set; }
public override FirstEnum Value { get; set; }
}
If you know that you will need only enums, you can add struct, IConvertible restriction to T:
public abstract class AbstractClass<T> where T : struct, IConvertible
{
public abstract string Name { get; set; }
public abstract T Value { get; set; }
}
Update based on comment:
Not the cleanest solution if you need List<AbstractClass>, but you can have additional class:
public abstract class AbstractClass
{
public abstract string Name { get; set; }
public abstract int GetValue ();
}
Which will then be inherited by AbstractClass<T>:
public abstract class AbstractClass<T> : AbstractClass where T : struct, IConvertible
{
public abstract T Value { get; set; }
}
And InheritancClass:
public class InheritanceClass1 : AbstractClass<FirstEnum>
{
public override string Name { get; set; }
public override FirstEnum Value { get; set; }
public override int GetValue () => (int)Value;
}
And then you can use it in a list:
var list = new List<AbstractClass> { new InheritanceClass1 (), new InheritanceClass2 () };
In this way you can use List<AbstractClass> with GetValue method. If you are using only enums you can always recast it to enum value. Ofcorse, you would not know exactly which enum it is, but you can add additional field for that.
I have the following two classes:
abstract class LogItem {
public String payload { get; set; }
public String serverId { get; set; }
public DateTime timeRecieved { get; set; }
}
class MyLogItem : LogItem
{
//No I want this to have to have the members from the abstract class above, as if it where an interface?
}
So in other words I am wanting a type if interface that can have definitions or variables which all classes that implement it have to have, but they could add more if they required ?
The above example builds, even if i dono add the members from the abstract class.
edit
Forget what I've said before. These are attributes, not methods. For them to be accessible on derived classes, you make them protected or public. The difference is that public members are visible to the world, while protected ones are visible to the class and subclasses.
Any class derived from your LogItem may have other variables.
abstract class LogItem {
public String payload { get; set; }
public String serverId { get; set; }
public DateTime timeRecieved { get; set; }
}
class MyLogItem : LogItem
{
//No I want this to have to have the members from the abstract class above, as if it where an interface?
private void TestMethod(){
String test = payload;
}
}
check out this post for more information
Your MyLogItem class can reference any of the above members directly. They are accessible
You may declare an interface with those
public interface MyInterface {
public String payload { get; set; }
public String serverId { get; set; }
public DateTime timeRecieved { get; set; }
}
and your class
public class MyLogItem : MyInterface
{
String _payload;
public String payload { get{ return _payload; } set {_payload=value;} }
...
}
The abstract keyword can also be applied to methods, as described here.