Returning a parent class - Inheritance

aaarianme

I have a parent class

public class User
{
    public int UserKey { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

I also have the class below that inherits from User

public class MyInfo : User
{
     public string Name { get; set; }
}

I have a utility class that returns a type of User. However I want to use the same method for MyInfo and only fill out whatever MyInfo inherited from User. The method can be found below:

public static User fetchUserInformation()
{
    User user = new User()
    //fetch data..
    return user;
}

What I’m doing is

MyInfo myinfo = fetchUserInformation();

but this wouldn’t work since fetchUserInformation returns a type of User.

Yiyi You

You can try to serialize User and deserialize it to MyInfo.

Change

MyInfo myinfo = fetchUserInformation();

to

MyInfo myInfo = JsonConvert.DeserializeObject<MyInfo>(JsonConvert.SerializeObject(fetchUserInformation()));

Another way,you can try without using inheritance.Here is a demo:

public class MyInfo
    {
        public string name { get; set; }
        public User user { get; set; }

    }
MyInfo myInfo = new MyInfo { user = fetchUserInformation() };

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Inheritance: Attribute from parent class

Inheritance - Return extended class from parent class

Returning a child class as their abstract parent class

python inheritance: choose parent class using argument

Python inheritance: create child object in parent class

What are the arguments in the parent class of an inheritance in Typescript?

Laravel parent class property inheritance problem

Inheritance: how to call method of parent class?

Why TypeScript complains about parent class inheritance?

Instantiating an Inheritance Object Class with Parent Properties

Scala inheritance default parameter in parent class

Inheritance in C++ (Parent and Child class)

Multiple Inheritance -- super() vs Parent class names

Inheritance class in different files parent “this” result to undefined

Python inheritance: modify a parent class of an object

Python AttributeError: Class Inheritance From a Parent Function

PHP inheritance methods or parent class name

Basic Inheritance in PHP Child & Parent Class

Python Parent class data access inheritance

Setup automatic inheritance of parent class' subclass in Python

Class Inheritance, using Child Class to set up Parent Class

Python Class Inheritance: How to initialize a subclass with values not in the parent class

Java inheritance: is the parent class instantiantiated every time a child class extends it?

Create inheritance class and print out data from the parent class array

How use class-based views inheritance to override to parent class?

In Inheritance method the Child class not receiving the URL from the Parent class

Class Method Not Returning Value When Accessed Via Inheritance

Objective-C class methods returning parent class objects

Flow type: use extended class from function returning parent class