Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:42:07+00:00 2026-06-17T20:42:07+00:00

I have a generic function like this: private LOCAL_TYPE RemoteToLocal<LOCAL_TYPE>(RemoteObjectBaseType remoteObject) where LOCAL_TYPE: EntityBase

  • 0

I have a generic function like this:

private LOCAL_TYPE RemoteToLocal<LOCAL_TYPE>(RemoteObjectBaseType remoteObject)
        where LOCAL_TYPE: EntityBase
    {
        Type t = typeof(LOCAL_TYPE);
        if (t == typeof(FavoritePlace))
        {
            return new FavoritePlace(remoteObject as RemotePlaceType1);
        }
    }

Where EntityBase is non-abstract class. FavoritePlace class is inherited from EntityBase.

However, I’m getting an error:

Cannot implicitly convert type Common.Model.FavoritePlace to ‘LOCAL_TYPE’.

That makes me wonder: FavoritePlace is a child of EntityBase, and LOCAL_TYPE is constrained to be of type EntityBase. Why cannot the conversion happen? I’m probably missing something important here.

EDIT: Okay, based on current answers and some experiment I’ve found another workaround, which is to do following conversion:

if (t == typeof(FavoritePlace))
    {
        return (LOCAL_TYPE)(EntityBase)new FavoritePlace(remoteObject);
    }

Now compiler is happy. But I’m just wondering, if such conversion is possible from compiler’s perspective, why direct conversion to LOCAL_TYPE is not? Isn’t is convertible to relationship transitive?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:42:08+00:00Added an answer on June 17, 2026 at 8:42 pm

    Although you have established through run-time code that LOCAL_TYPE is in fact FavoritePlace, the compiler does not have the same knowledge statically. The compiler expects you to return an object of type LOCAL_TYPE, matching exactly the type parameter of the method.

    Thinks of this situation: someone makes the following call –

    var res = RemoteToLocal<MostHatedPlace>(remoteObj); // MostHatedPlace derives from EntityBase
    

    Now you’re inside RemoteToLocal, you go through some conditions, and now it’s time to return the result. You call

    return new FavoritePlace(remoteObject as RemotePlaceType1);
    

    You know that this branch in code is impossible to reach, because there is a run-time check guarding you from that:

    if (t == typeof(FavoritePlace)) {
        ....
    }
    

    However, the compiler must assume that reaching this return statement is possible, which would be an error in cases when LOCAL_TYPE is not a FavoritePlace.

    You may want to reconsider the use of generics here: from the code snippet it appears that you need the generic argument to avoid type-casting the result to the desired type in the caller. However, the caller would then need to perform an additional check to see if the conversion inside your RemoteToLocal has succeeded. In this case, a method

    private EntityBase RemoteToLocal(RemoteObjectBaseType remoteObject) {
        ....
    }
    

    may be equally suited to the task, because it would be free of conversions that trick the compiler, and the structure of the calling code would remain the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that accepts a generic parameter T that is of type
I have a method like this: private static double ComputePercentage(ushort level, ushort capacity) {
I have this function: private void button6_Click(object sender, EventArgs e) { crawlLocaly1 = new
I have some function like private static UserInfo FillUserInfoFromDataRow(DataRow dr) { UserInfo user =
I am working with EF generic repository and have this function. public IEnumerable<T> Query(Expression<Func<T,
I have an Action delegate declared like this. private readonly Action<List<PSDPoint>[]> _psdAction; _psdAction =
I want to create a generic function with signature like this : void funcName<T>()
I have a label function like : private function formatDate (item:Object, column:DataGridColumn):String { var
I have a function like this: def foo(bar): ... Now bar can either be
I'd like to make this function more generic by specifying the property of Foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.