简体   繁体   中英

How to create generic property in model class using asp.net core

public class A
{
   public int Val {get; set;}
   public int B {get; set;}
   public T C{get; set;} // make generic property
}

How can I create generic property in model class using asp.net core. In above code I want to make

"C" property as a generic property and convert that property into particular class.

In "C" will be dynamic property.

Example

public Class D
{
  public int Value1 {get; set;}
}

public Class E
{
  public int Value2 {get; set;}
}

The "C" property have either D or E

public class A<T>
{
   public int A {get; set;}
   public int B {get; set;}
   public T C {get; set;} 
}

And use it like this:

var a = new A<D>() 

OR

var a = new A<E>()

Generic classes and methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM