How do I alter this code to allow abstract classes or interfaces to work over identical auto generated...











up vote
0
down vote

favorite












Explanation



I use the xsd.exe tool to generate a class API based upon an XSD (defines a schema for XML files) from which to interact with XML files.



This tool works well, but this issue is that I have a few schemas which are by-and-large identical with small tweaks, so I want to create interfaces or abstract classes that allow me to reuse code elsewhere.



In my example below I've simplified the generated code for purposes of sharing it here but the principle still holds.



Example Non-Functioning Code



Program.cs



public static void Main()
{
BaseColor baseColor = new Color1 { ColorDescription = "Red" };
BaseShape baseShape = new Shape1 { Content = baseColor };
}


Models.cs



//Auto generated models - I have no control over these but they are partial classes

//First set of autogenerated models, normally in its own file
public partial class Shape1
{
public Color1 Content { get; set; }
}

public partial class Color1
{

public string ColorDescription { get; set; }
}

//Second set of autogenerated models, normally in its own file
public partial class Shape2
{
public Color2 Content { get; set; }
}

public partial class Color2
{

public string ColorDescription { get; set; }
}

//Attemping to abstract these classes so I can generically use them regardless of underlying type
public abstract class BaseShape
{
public abstract BaseColor Content { get; set; }
}

public abstract class BaseColor
{
public abstract string ColorDescription { get; set; }
}

//Attempting to extend the autogenerated classes with the abstract classes
public partial class Shape1 : BaseShape { }

public partial class Color1 : BaseColor { }

public partial class Shape2 : BaseShape { }

public partial class Color2 : BaseColor { }


Errors



This error is repeated 8 times in total for both shapes, both colors and both get/set methods.



'Shape1' does not implement inherited abstract member 'BaseShape.Content.set'   XmlSerializeChild


And from the Main method.



Cannot implicitly convert type 'XmlSerializeChild.Models.BaseColor' to 'XmlSerializeChild.Models.Color1'. An explicit conversion exists (are you missing a cast?)









share|improve this question






















  • You cant change the types when overriding also you will need the override keyword, you are going to have to rethink this
    – TheGeneral
    Nov 8 at 10:59












  • Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
    – Geesh_SO
    Nov 8 at 11:02















up vote
0
down vote

favorite












Explanation



I use the xsd.exe tool to generate a class API based upon an XSD (defines a schema for XML files) from which to interact with XML files.



This tool works well, but this issue is that I have a few schemas which are by-and-large identical with small tweaks, so I want to create interfaces or abstract classes that allow me to reuse code elsewhere.



In my example below I've simplified the generated code for purposes of sharing it here but the principle still holds.



Example Non-Functioning Code



Program.cs



public static void Main()
{
BaseColor baseColor = new Color1 { ColorDescription = "Red" };
BaseShape baseShape = new Shape1 { Content = baseColor };
}


Models.cs



//Auto generated models - I have no control over these but they are partial classes

//First set of autogenerated models, normally in its own file
public partial class Shape1
{
public Color1 Content { get; set; }
}

public partial class Color1
{

public string ColorDescription { get; set; }
}

//Second set of autogenerated models, normally in its own file
public partial class Shape2
{
public Color2 Content { get; set; }
}

public partial class Color2
{

public string ColorDescription { get; set; }
}

//Attemping to abstract these classes so I can generically use them regardless of underlying type
public abstract class BaseShape
{
public abstract BaseColor Content { get; set; }
}

public abstract class BaseColor
{
public abstract string ColorDescription { get; set; }
}

//Attempting to extend the autogenerated classes with the abstract classes
public partial class Shape1 : BaseShape { }

public partial class Color1 : BaseColor { }

public partial class Shape2 : BaseShape { }

public partial class Color2 : BaseColor { }


Errors



This error is repeated 8 times in total for both shapes, both colors and both get/set methods.



'Shape1' does not implement inherited abstract member 'BaseShape.Content.set'   XmlSerializeChild


And from the Main method.



Cannot implicitly convert type 'XmlSerializeChild.Models.BaseColor' to 'XmlSerializeChild.Models.Color1'. An explicit conversion exists (are you missing a cast?)









share|improve this question






















  • You cant change the types when overriding also you will need the override keyword, you are going to have to rethink this
    – TheGeneral
    Nov 8 at 10:59












  • Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
    – Geesh_SO
    Nov 8 at 11:02













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Explanation



I use the xsd.exe tool to generate a class API based upon an XSD (defines a schema for XML files) from which to interact with XML files.



This tool works well, but this issue is that I have a few schemas which are by-and-large identical with small tweaks, so I want to create interfaces or abstract classes that allow me to reuse code elsewhere.



In my example below I've simplified the generated code for purposes of sharing it here but the principle still holds.



Example Non-Functioning Code



Program.cs



public static void Main()
{
BaseColor baseColor = new Color1 { ColorDescription = "Red" };
BaseShape baseShape = new Shape1 { Content = baseColor };
}


Models.cs



//Auto generated models - I have no control over these but they are partial classes

//First set of autogenerated models, normally in its own file
public partial class Shape1
{
public Color1 Content { get; set; }
}

public partial class Color1
{

public string ColorDescription { get; set; }
}

//Second set of autogenerated models, normally in its own file
public partial class Shape2
{
public Color2 Content { get; set; }
}

public partial class Color2
{

public string ColorDescription { get; set; }
}

//Attemping to abstract these classes so I can generically use them regardless of underlying type
public abstract class BaseShape
{
public abstract BaseColor Content { get; set; }
}

public abstract class BaseColor
{
public abstract string ColorDescription { get; set; }
}

//Attempting to extend the autogenerated classes with the abstract classes
public partial class Shape1 : BaseShape { }

public partial class Color1 : BaseColor { }

public partial class Shape2 : BaseShape { }

public partial class Color2 : BaseColor { }


Errors



This error is repeated 8 times in total for both shapes, both colors and both get/set methods.



'Shape1' does not implement inherited abstract member 'BaseShape.Content.set'   XmlSerializeChild


And from the Main method.



Cannot implicitly convert type 'XmlSerializeChild.Models.BaseColor' to 'XmlSerializeChild.Models.Color1'. An explicit conversion exists (are you missing a cast?)









share|improve this question













Explanation



I use the xsd.exe tool to generate a class API based upon an XSD (defines a schema for XML files) from which to interact with XML files.



This tool works well, but this issue is that I have a few schemas which are by-and-large identical with small tweaks, so I want to create interfaces or abstract classes that allow me to reuse code elsewhere.



In my example below I've simplified the generated code for purposes of sharing it here but the principle still holds.



Example Non-Functioning Code



Program.cs



public static void Main()
{
BaseColor baseColor = new Color1 { ColorDescription = "Red" };
BaseShape baseShape = new Shape1 { Content = baseColor };
}


Models.cs



//Auto generated models - I have no control over these but they are partial classes

//First set of autogenerated models, normally in its own file
public partial class Shape1
{
public Color1 Content { get; set; }
}

public partial class Color1
{

public string ColorDescription { get; set; }
}

//Second set of autogenerated models, normally in its own file
public partial class Shape2
{
public Color2 Content { get; set; }
}

public partial class Color2
{

public string ColorDescription { get; set; }
}

//Attemping to abstract these classes so I can generically use them regardless of underlying type
public abstract class BaseShape
{
public abstract BaseColor Content { get; set; }
}

public abstract class BaseColor
{
public abstract string ColorDescription { get; set; }
}

//Attempting to extend the autogenerated classes with the abstract classes
public partial class Shape1 : BaseShape { }

public partial class Color1 : BaseColor { }

public partial class Shape2 : BaseShape { }

public partial class Color2 : BaseColor { }


Errors



This error is repeated 8 times in total for both shapes, both colors and both get/set methods.



'Shape1' does not implement inherited abstract member 'BaseShape.Content.set'   XmlSerializeChild


And from the Main method.



Cannot implicitly convert type 'XmlSerializeChild.Models.BaseColor' to 'XmlSerializeChild.Models.Color1'. An explicit conversion exists (are you missing a cast?)






c# oop inheritance interface abstract-class






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 10:48









Geesh_SO

52021136




52021136












  • You cant change the types when overriding also you will need the override keyword, you are going to have to rethink this
    – TheGeneral
    Nov 8 at 10:59












  • Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
    – Geesh_SO
    Nov 8 at 11:02


















  • You cant change the types when overriding also you will need the override keyword, you are going to have to rethink this
    – TheGeneral
    Nov 8 at 10:59












  • Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
    – Geesh_SO
    Nov 8 at 11:02
















You cant change the types when overriding also you will need the override keyword, you are going to have to rethink this
– TheGeneral
Nov 8 at 10:59






You cant change the types when overriding also you will need the override keyword, you are going to have to rethink this
– TheGeneral
Nov 8 at 10:59














Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
– Geesh_SO
Nov 8 at 11:02




Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
– Geesh_SO
Nov 8 at 11:02












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You where quite close, but as TheGeneral wrote in his comment, you can't change the type of the property when overriding it.



What you can do is introduce a new property (I've chosen to use interfaces but it will work just as well with abstract classes) that will be used in the code, having the explicit cast in each partial class:



So first, I've created the interfaces:



public interface IColor { string ColorDescription { get; set; } }

public interface IShape { IColor BaseContent { get; set; } }


Then, added the IColor implementation to the Color1 and Color2 classes:



public partial class Color1 : IColor {}
public partial class Color2 : IColor {}


(That was the easy part since the ColorDescription is the same type for both colors).



Next, I've added the IShape implementation to the Shape1 and Shape2 classes:



public partial class Shape1 : IShape 
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color2) value; }
}
}


Now, in the Main method, you can do this:



var baseColor = new Color1() { ColorDescription = "Red" };
var baseShape = new Shape1() { BaseContent = baseColor };


Another option instead of introducing a new property would be to implicitly implement the IShape interface - but this would be more cumbersome and will not allow you to use the new Shape1() {Content = baseColor} syntax. Still, let's review this option as well:



So we rename the BaseContent property in the IShape interface:



interface IShape { IColor Content { get; set; } }


And we implement it like this:



public partial class Shape1 : IShape 
{
IColor IShape.Content
{
get { return ((Shape1)this).Content; }
set { ((Shape1)this).Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
IColor IShape.Content
{
get { return ((Shape2)this).Content; }
set { ((Shape2)this).Content = (Color2) value; }
}
}


Then, we create our reverences like this:



var baseColor = new Color1() { ColorDescription = "Red" };
// Note: Do not use var here - you need the reference to be of type `IShape`!
IShape baseShape = new Shape1();
baseShape.Content = baseColor;





share|improve this answer























  • argggg you beat me, upvote, also with less mucking about
    – TheGeneral
    Nov 8 at 11:23












  • @TheGeneral You win some you lose some...
    – Zohar Peled
    Nov 8 at 11:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206140%2fhow-do-i-alter-this-code-to-allow-abstract-classes-or-interfaces-to-work-over-id%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You where quite close, but as TheGeneral wrote in his comment, you can't change the type of the property when overriding it.



What you can do is introduce a new property (I've chosen to use interfaces but it will work just as well with abstract classes) that will be used in the code, having the explicit cast in each partial class:



So first, I've created the interfaces:



public interface IColor { string ColorDescription { get; set; } }

public interface IShape { IColor BaseContent { get; set; } }


Then, added the IColor implementation to the Color1 and Color2 classes:



public partial class Color1 : IColor {}
public partial class Color2 : IColor {}


(That was the easy part since the ColorDescription is the same type for both colors).



Next, I've added the IShape implementation to the Shape1 and Shape2 classes:



public partial class Shape1 : IShape 
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color2) value; }
}
}


Now, in the Main method, you can do this:



var baseColor = new Color1() { ColorDescription = "Red" };
var baseShape = new Shape1() { BaseContent = baseColor };


Another option instead of introducing a new property would be to implicitly implement the IShape interface - but this would be more cumbersome and will not allow you to use the new Shape1() {Content = baseColor} syntax. Still, let's review this option as well:



So we rename the BaseContent property in the IShape interface:



interface IShape { IColor Content { get; set; } }


And we implement it like this:



public partial class Shape1 : IShape 
{
IColor IShape.Content
{
get { return ((Shape1)this).Content; }
set { ((Shape1)this).Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
IColor IShape.Content
{
get { return ((Shape2)this).Content; }
set { ((Shape2)this).Content = (Color2) value; }
}
}


Then, we create our reverences like this:



var baseColor = new Color1() { ColorDescription = "Red" };
// Note: Do not use var here - you need the reference to be of type `IShape`!
IShape baseShape = new Shape1();
baseShape.Content = baseColor;





share|improve this answer























  • argggg you beat me, upvote, also with less mucking about
    – TheGeneral
    Nov 8 at 11:23












  • @TheGeneral You win some you lose some...
    – Zohar Peled
    Nov 8 at 11:30















up vote
2
down vote



accepted










You where quite close, but as TheGeneral wrote in his comment, you can't change the type of the property when overriding it.



What you can do is introduce a new property (I've chosen to use interfaces but it will work just as well with abstract classes) that will be used in the code, having the explicit cast in each partial class:



So first, I've created the interfaces:



public interface IColor { string ColorDescription { get; set; } }

public interface IShape { IColor BaseContent { get; set; } }


Then, added the IColor implementation to the Color1 and Color2 classes:



public partial class Color1 : IColor {}
public partial class Color2 : IColor {}


(That was the easy part since the ColorDescription is the same type for both colors).



Next, I've added the IShape implementation to the Shape1 and Shape2 classes:



public partial class Shape1 : IShape 
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color2) value; }
}
}


Now, in the Main method, you can do this:



var baseColor = new Color1() { ColorDescription = "Red" };
var baseShape = new Shape1() { BaseContent = baseColor };


Another option instead of introducing a new property would be to implicitly implement the IShape interface - but this would be more cumbersome and will not allow you to use the new Shape1() {Content = baseColor} syntax. Still, let's review this option as well:



So we rename the BaseContent property in the IShape interface:



interface IShape { IColor Content { get; set; } }


And we implement it like this:



public partial class Shape1 : IShape 
{
IColor IShape.Content
{
get { return ((Shape1)this).Content; }
set { ((Shape1)this).Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
IColor IShape.Content
{
get { return ((Shape2)this).Content; }
set { ((Shape2)this).Content = (Color2) value; }
}
}


Then, we create our reverences like this:



var baseColor = new Color1() { ColorDescription = "Red" };
// Note: Do not use var here - you need the reference to be of type `IShape`!
IShape baseShape = new Shape1();
baseShape.Content = baseColor;





share|improve this answer























  • argggg you beat me, upvote, also with less mucking about
    – TheGeneral
    Nov 8 at 11:23












  • @TheGeneral You win some you lose some...
    – Zohar Peled
    Nov 8 at 11:30













up vote
2
down vote



accepted







up vote
2
down vote



accepted






You where quite close, but as TheGeneral wrote in his comment, you can't change the type of the property when overriding it.



What you can do is introduce a new property (I've chosen to use interfaces but it will work just as well with abstract classes) that will be used in the code, having the explicit cast in each partial class:



So first, I've created the interfaces:



public interface IColor { string ColorDescription { get; set; } }

public interface IShape { IColor BaseContent { get; set; } }


Then, added the IColor implementation to the Color1 and Color2 classes:



public partial class Color1 : IColor {}
public partial class Color2 : IColor {}


(That was the easy part since the ColorDescription is the same type for both colors).



Next, I've added the IShape implementation to the Shape1 and Shape2 classes:



public partial class Shape1 : IShape 
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color2) value; }
}
}


Now, in the Main method, you can do this:



var baseColor = new Color1() { ColorDescription = "Red" };
var baseShape = new Shape1() { BaseContent = baseColor };


Another option instead of introducing a new property would be to implicitly implement the IShape interface - but this would be more cumbersome and will not allow you to use the new Shape1() {Content = baseColor} syntax. Still, let's review this option as well:



So we rename the BaseContent property in the IShape interface:



interface IShape { IColor Content { get; set; } }


And we implement it like this:



public partial class Shape1 : IShape 
{
IColor IShape.Content
{
get { return ((Shape1)this).Content; }
set { ((Shape1)this).Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
IColor IShape.Content
{
get { return ((Shape2)this).Content; }
set { ((Shape2)this).Content = (Color2) value; }
}
}


Then, we create our reverences like this:



var baseColor = new Color1() { ColorDescription = "Red" };
// Note: Do not use var here - you need the reference to be of type `IShape`!
IShape baseShape = new Shape1();
baseShape.Content = baseColor;





share|improve this answer














You where quite close, but as TheGeneral wrote in his comment, you can't change the type of the property when overriding it.



What you can do is introduce a new property (I've chosen to use interfaces but it will work just as well with abstract classes) that will be used in the code, having the explicit cast in each partial class:



So first, I've created the interfaces:



public interface IColor { string ColorDescription { get; set; } }

public interface IShape { IColor BaseContent { get; set; } }


Then, added the IColor implementation to the Color1 and Color2 classes:



public partial class Color1 : IColor {}
public partial class Color2 : IColor {}


(That was the easy part since the ColorDescription is the same type for both colors).



Next, I've added the IShape implementation to the Shape1 and Shape2 classes:



public partial class Shape1 : IShape 
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
public IColor BaseContent
{
get { return Content; }
set { Content = (Color2) value; }
}
}


Now, in the Main method, you can do this:



var baseColor = new Color1() { ColorDescription = "Red" };
var baseShape = new Shape1() { BaseContent = baseColor };


Another option instead of introducing a new property would be to implicitly implement the IShape interface - but this would be more cumbersome and will not allow you to use the new Shape1() {Content = baseColor} syntax. Still, let's review this option as well:



So we rename the BaseContent property in the IShape interface:



interface IShape { IColor Content { get; set; } }


And we implement it like this:



public partial class Shape1 : IShape 
{
IColor IShape.Content
{
get { return ((Shape1)this).Content; }
set { ((Shape1)this).Content = (Color1) value; }
}
}

public partial class Shape2 : IShape
{
IColor IShape.Content
{
get { return ((Shape2)this).Content; }
set { ((Shape2)this).Content = (Color2) value; }
}
}


Then, we create our reverences like this:



var baseColor = new Color1() { ColorDescription = "Red" };
// Note: Do not use var here - you need the reference to be of type `IShape`!
IShape baseShape = new Shape1();
baseShape.Content = baseColor;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 11:25

























answered Nov 8 at 11:19









Zohar Peled

50.8k73171




50.8k73171












  • argggg you beat me, upvote, also with less mucking about
    – TheGeneral
    Nov 8 at 11:23












  • @TheGeneral You win some you lose some...
    – Zohar Peled
    Nov 8 at 11:30


















  • argggg you beat me, upvote, also with less mucking about
    – TheGeneral
    Nov 8 at 11:23












  • @TheGeneral You win some you lose some...
    – Zohar Peled
    Nov 8 at 11:30
















argggg you beat me, upvote, also with less mucking about
– TheGeneral
Nov 8 at 11:23






argggg you beat me, upvote, also with less mucking about
– TheGeneral
Nov 8 at 11:23














@TheGeneral You win some you lose some...
– Zohar Peled
Nov 8 at 11:30




@TheGeneral You win some you lose some...
– Zohar Peled
Nov 8 at 11:30


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206140%2fhow-do-i-alter-this-code-to-allow-abstract-classes-or-interfaces-to-work-over-id%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff