How do I alter this code to allow abstract classes or interfaces to work over identical auto generated classes?
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?)
c# oop inheritance interface abstract-class
add a comment |
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?)
c# oop inheritance interface abstract-class
You cant change the types when overriding also you will need theoverride
keyword, you are going to have to rethink this
– TheGeneral
2 days ago
Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
– Geesh_SO
2 days ago
add a comment |
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?)
c# oop inheritance interface abstract-class
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
c# oop inheritance interface abstract-class
asked 2 days ago
Geesh_SO
52021136
52021136
You cant change the types when overriding also you will need theoverride
keyword, you are going to have to rethink this
– TheGeneral
2 days ago
Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
– Geesh_SO
2 days ago
add a comment |
You cant change the types when overriding also you will need theoverride
keyword, you are going to have to rethink this
– TheGeneral
2 days ago
Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
– Geesh_SO
2 days ago
You cant change the types when overriding also you will need the
override
keyword, you are going to have to rethink this– TheGeneral
2 days ago
You cant change the types when overriding also you will need the
override
keyword, you are going to have to rethink this– TheGeneral
2 days ago
Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
– Geesh_SO
2 days ago
Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
– Geesh_SO
2 days ago
add a comment |
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;
argggg you beat me, upvote, also with less mucking about
– TheGeneral
2 days ago
@TheGeneral You win some you lose some...
– Zohar Peled
2 days ago
add a comment |
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;
argggg you beat me, upvote, also with less mucking about
– TheGeneral
2 days ago
@TheGeneral You win some you lose some...
– Zohar Peled
2 days ago
add a comment |
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;
argggg you beat me, upvote, also with less mucking about
– TheGeneral
2 days ago
@TheGeneral You win some you lose some...
– Zohar Peled
2 days ago
add a comment |
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;
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;
edited 2 days ago
answered 2 days ago
Zohar Peled
50.3k73070
50.3k73070
argggg you beat me, upvote, also with less mucking about
– TheGeneral
2 days ago
@TheGeneral You win some you lose some...
– Zohar Peled
2 days ago
add a comment |
argggg you beat me, upvote, also with less mucking about
– TheGeneral
2 days ago
@TheGeneral You win some you lose some...
– Zohar Peled
2 days ago
argggg you beat me, upvote, also with less mucking about
– TheGeneral
2 days ago
argggg you beat me, upvote, also with less mucking about
– TheGeneral
2 days ago
@TheGeneral You win some you lose some...
– Zohar Peled
2 days ago
@TheGeneral You win some you lose some...
– Zohar Peled
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You cant change the types when overriding also you will need the
override
keyword, you are going to have to rethink this– TheGeneral
2 days ago
Any thoughts on a different approach? The only constraint that I have is that I can't modify the auto-generated files.
– Geesh_SO
2 days ago