How do I cast Class FooObject to class BarObject which both implement interface IObject? [duplicate]2019 Community Moderator ElectionCasting between two types derived from the (same) interfaceCasting between classes that share the same interfaceCasting a interface derived class to another derived classcast class into another class or convert class to anotherInterface vs Base classC# Interfaces. Implicit implementation versus Explicit implementationHow do you declare an interface in C++?Interface vs Abstract Class (general OO)How to convert DateTime? to DateTimeWhat is the difference between an interface and abstract class?How to determine if a type implements an interface with C# reflectionC# internal interface with internal implementationC# 3.0 implicit cast error with class and interfaces with generic typeCasting object to Implemented Interfaces
Happy pi day, everyone!
Is it insecure to send a password in a `curl` command?
What does 高層ビルに何車線もの道路。mean?
et qui - how do you really understand that kind of phraseology?
Why does a Star of David appear at a rally with Francisco Franco?
Different outputs for `w`, `who`, `whoami` and `id`
What are substitutions for coconut in curry?
This word with a lot of past tenses
As a new Ubuntu desktop 18.04 LTS user, do I need to use ufw for a firewall or is iptables sufficient?
Is it good practice to use Linear Least-Squares with SMA?
Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?
Meme-controlled people
Instead of a Universal Basic Income program, why not implement a "Universal Basic Needs" program?
Why do passenger jet manufacturers design their planes with stall prevention systems?
What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?
Why one should not leave fingerprints on bulbs and plugs?
What is "focus distance lower/upper" and how is it different from depth of field?
Have the tides ever turned twice on any open problem?
Brexit - No Deal Rejection
How to deal with taxi scam when on vacation?
How to plot polar formed complex numbers?
Knife as defense against stray dogs
How do I hide Chekhov's Gun?
If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?
How do I cast Class FooObject to class BarObject which both implement interface IObject? [duplicate]
2019 Community Moderator ElectionCasting between two types derived from the (same) interfaceCasting between classes that share the same interfaceCasting a interface derived class to another derived classcast class into another class or convert class to anotherInterface vs Base classC# Interfaces. Implicit implementation versus Explicit implementationHow do you declare an interface in C++?Interface vs Abstract Class (general OO)How to convert DateTime? to DateTimeWhat is the difference between an interface and abstract class?How to determine if a type implements an interface with C# reflectionC# internal interface with internal implementationC# 3.0 implicit cast error with class and interfaces with generic typeCasting object to Implemented Interfaces
This question already has an answer here:
Casting between two types derived from the (same) interface
9 answers
Casting between classes that share the same interface
7 answers
Casting a interface derived class to another derived class
1 answer
I have 2 classes which implement the same interface. But somehow I cannot cast them from one to another.
Here's an example:
public interface IObject
// ...
public class FooObject : IObject
// ...
public class BarObject : IObject
// ...
-
If I do it like this, VS will mark it as Cannot convert type 'FooObject' to 'BarObject'
var foo = new FooObject();
var bar = (BarObject)foo; // Build error
-
If I do it like this, there is no build error, but when called, it throw System.InvalidCastException: Unable to cast object of type 'FooObject' to 'BarObject'.
var foo = new FooObject();
var bar = (BarObject)(IObject)foo; // No build error, but InvalidCastException gets thrown
-
So, how do I cast/parse/convert an implementation (FooObject
) of an interface (IObject
) to another implementation (BarObject
) of that same interface?
Solution:
I fixed this by creating an explicit conversion operator:
public class FooObject
// ...
public static explicit operator BarObject(FooObject fooObject)
// ...
Thanks to CodeCaster.
c# interface casting implementation
marked as duplicate by CodeCaster
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 7 at 15:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Casting between two types derived from the (same) interface
9 answers
Casting between classes that share the same interface
7 answers
Casting a interface derived class to another derived class
1 answer
I have 2 classes which implement the same interface. But somehow I cannot cast them from one to another.
Here's an example:
public interface IObject
// ...
public class FooObject : IObject
// ...
public class BarObject : IObject
// ...
-
If I do it like this, VS will mark it as Cannot convert type 'FooObject' to 'BarObject'
var foo = new FooObject();
var bar = (BarObject)foo; // Build error
-
If I do it like this, there is no build error, but when called, it throw System.InvalidCastException: Unable to cast object of type 'FooObject' to 'BarObject'.
var foo = new FooObject();
var bar = (BarObject)(IObject)foo; // No build error, but InvalidCastException gets thrown
-
So, how do I cast/parse/convert an implementation (FooObject
) of an interface (IObject
) to another implementation (BarObject
) of that same interface?
Solution:
I fixed this by creating an explicit conversion operator:
public class FooObject
// ...
public static explicit operator BarObject(FooObject fooObject)
// ...
Thanks to CodeCaster.
c# interface casting implementation
marked as duplicate by CodeCaster
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 7 at 15:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
You can't. BothCat
andDog
can implementIAnimal
, but you can't cast one to the other - at least not without writing implicit or explicit conversion operators. What problem are you actually trying to solve?
– CodeCaster
Mar 7 at 15:31
@CodeCaster, I created apublic static explicit operator BarObject(FooObject fooObject)
and that did the trick. Thanks!
– Mason
Mar 7 at 15:41
1
I just made a similar suggestion here: stackoverflow.com/a/55047697/880990.
– Olivier Jacot-Descombes
Mar 7 at 15:51
If you use this extension method, you probably will be right: stackoverflow.com/questions/3672742/…
– MohammadReza Hasanzadeh
Mar 7 at 16:15
1
@Olivier nice, that's what that duplicate was missing.
– CodeCaster
Mar 7 at 16:23
add a comment |
This question already has an answer here:
Casting between two types derived from the (same) interface
9 answers
Casting between classes that share the same interface
7 answers
Casting a interface derived class to another derived class
1 answer
I have 2 classes which implement the same interface. But somehow I cannot cast them from one to another.
Here's an example:
public interface IObject
// ...
public class FooObject : IObject
// ...
public class BarObject : IObject
// ...
-
If I do it like this, VS will mark it as Cannot convert type 'FooObject' to 'BarObject'
var foo = new FooObject();
var bar = (BarObject)foo; // Build error
-
If I do it like this, there is no build error, but when called, it throw System.InvalidCastException: Unable to cast object of type 'FooObject' to 'BarObject'.
var foo = new FooObject();
var bar = (BarObject)(IObject)foo; // No build error, but InvalidCastException gets thrown
-
So, how do I cast/parse/convert an implementation (FooObject
) of an interface (IObject
) to another implementation (BarObject
) of that same interface?
Solution:
I fixed this by creating an explicit conversion operator:
public class FooObject
// ...
public static explicit operator BarObject(FooObject fooObject)
// ...
Thanks to CodeCaster.
c# interface casting implementation
This question already has an answer here:
Casting between two types derived from the (same) interface
9 answers
Casting between classes that share the same interface
7 answers
Casting a interface derived class to another derived class
1 answer
I have 2 classes which implement the same interface. But somehow I cannot cast them from one to another.
Here's an example:
public interface IObject
// ...
public class FooObject : IObject
// ...
public class BarObject : IObject
// ...
-
If I do it like this, VS will mark it as Cannot convert type 'FooObject' to 'BarObject'
var foo = new FooObject();
var bar = (BarObject)foo; // Build error
-
If I do it like this, there is no build error, but when called, it throw System.InvalidCastException: Unable to cast object of type 'FooObject' to 'BarObject'.
var foo = new FooObject();
var bar = (BarObject)(IObject)foo; // No build error, but InvalidCastException gets thrown
-
So, how do I cast/parse/convert an implementation (FooObject
) of an interface (IObject
) to another implementation (BarObject
) of that same interface?
Solution:
I fixed this by creating an explicit conversion operator:
public class FooObject
// ...
public static explicit operator BarObject(FooObject fooObject)
// ...
Thanks to CodeCaster.
This question already has an answer here:
Casting between two types derived from the (same) interface
9 answers
Casting between classes that share the same interface
7 answers
Casting a interface derived class to another derived class
1 answer
c# interface casting implementation
c# interface casting implementation
edited Mar 7 at 15:44
Mason
asked Mar 7 at 15:30
MasonMason
371117
371117
marked as duplicate by CodeCaster
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 7 at 15:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by CodeCaster
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 7 at 15:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
You can't. BothCat
andDog
can implementIAnimal
, but you can't cast one to the other - at least not without writing implicit or explicit conversion operators. What problem are you actually trying to solve?
– CodeCaster
Mar 7 at 15:31
@CodeCaster, I created apublic static explicit operator BarObject(FooObject fooObject)
and that did the trick. Thanks!
– Mason
Mar 7 at 15:41
1
I just made a similar suggestion here: stackoverflow.com/a/55047697/880990.
– Olivier Jacot-Descombes
Mar 7 at 15:51
If you use this extension method, you probably will be right: stackoverflow.com/questions/3672742/…
– MohammadReza Hasanzadeh
Mar 7 at 16:15
1
@Olivier nice, that's what that duplicate was missing.
– CodeCaster
Mar 7 at 16:23
add a comment |
2
You can't. BothCat
andDog
can implementIAnimal
, but you can't cast one to the other - at least not without writing implicit or explicit conversion operators. What problem are you actually trying to solve?
– CodeCaster
Mar 7 at 15:31
@CodeCaster, I created apublic static explicit operator BarObject(FooObject fooObject)
and that did the trick. Thanks!
– Mason
Mar 7 at 15:41
1
I just made a similar suggestion here: stackoverflow.com/a/55047697/880990.
– Olivier Jacot-Descombes
Mar 7 at 15:51
If you use this extension method, you probably will be right: stackoverflow.com/questions/3672742/…
– MohammadReza Hasanzadeh
Mar 7 at 16:15
1
@Olivier nice, that's what that duplicate was missing.
– CodeCaster
Mar 7 at 16:23
2
2
You can't. Both
Cat
and Dog
can implement IAnimal
, but you can't cast one to the other - at least not without writing implicit or explicit conversion operators. What problem are you actually trying to solve?– CodeCaster
Mar 7 at 15:31
You can't. Both
Cat
and Dog
can implement IAnimal
, but you can't cast one to the other - at least not without writing implicit or explicit conversion operators. What problem are you actually trying to solve?– CodeCaster
Mar 7 at 15:31
@CodeCaster, I created a
public static explicit operator BarObject(FooObject fooObject)
and that did the trick. Thanks!– Mason
Mar 7 at 15:41
@CodeCaster, I created a
public static explicit operator BarObject(FooObject fooObject)
and that did the trick. Thanks!– Mason
Mar 7 at 15:41
1
1
I just made a similar suggestion here: stackoverflow.com/a/55047697/880990.
– Olivier Jacot-Descombes
Mar 7 at 15:51
I just made a similar suggestion here: stackoverflow.com/a/55047697/880990.
– Olivier Jacot-Descombes
Mar 7 at 15:51
If you use this extension method, you probably will be right: stackoverflow.com/questions/3672742/…
– MohammadReza Hasanzadeh
Mar 7 at 16:15
If you use this extension method, you probably will be right: stackoverflow.com/questions/3672742/…
– MohammadReza Hasanzadeh
Mar 7 at 16:15
1
1
@Olivier nice, that's what that duplicate was missing.
– CodeCaster
Mar 7 at 16:23
@Olivier nice, that's what that duplicate was missing.
– CodeCaster
Mar 7 at 16:23
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
2
You can't. Both
Cat
andDog
can implementIAnimal
, but you can't cast one to the other - at least not without writing implicit or explicit conversion operators. What problem are you actually trying to solve?– CodeCaster
Mar 7 at 15:31
@CodeCaster, I created a
public static explicit operator BarObject(FooObject fooObject)
and that did the trick. Thanks!– Mason
Mar 7 at 15:41
1
I just made a similar suggestion here: stackoverflow.com/a/55047697/880990.
– Olivier Jacot-Descombes
Mar 7 at 15:51
If you use this extension method, you probably will be right: stackoverflow.com/questions/3672742/…
– MohammadReza Hasanzadeh
Mar 7 at 16:15
1
@Olivier nice, that's what that duplicate was missing.
– CodeCaster
Mar 7 at 16:23