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










3
















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.










share|improve this question















marked as duplicate by CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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. 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






  • 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















3
















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.










share|improve this question















marked as duplicate by CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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. 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






  • 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













3












3








3


2







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.










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 15:44







Mason

















asked Mar 7 at 15:30









MasonMason

371117




371117




marked as duplicate by CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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. 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






  • 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





    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






  • 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












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived