Home > Net >  Subclasses parent transformation problem
Subclasses parent transformation problem

Time:11-18

As follows:
Public class ParentClass
{

}
Public class ChildAClass: ParentClass
{

}
Public class ChildBClass: ParentClass
{

}

Public T GetChild (ParentClass PC, string classType)
{
//for expert advice on how to write, in this will return a generic
}

Parameters for the type of parent and child classes, output parameters for the subclasses of generics; Parameter in the PC to ensure can be converted into subclasses

CodePudding user response:

This writing don't you call a generic, generic is characterized by: specified type at runtime, you only have one T, the return value is that there is no specified type "" ah, you write the overall framework of has a problem, not just fill in the blanks of how to write the code, to change the framework code,

CodePudding user response:

I guess you want to get a subclass by classType Type (Type)? Because if just type conversion, you don't need to write like this, directly (PC as T)
Don't need a paradigm, do not need to pass the superclass object
 
Public static Type GetChild (string classType)
=> The Assembly
GetExecutingAssembly ()
The GetTypes ()
The Where (t=& gt; Typeof (ParentClass). IsAssignableFrom (t) & amp; & T.N ame==classType)
FirstOrDefault ();

CodePudding user response:

Since want to convert, that will define the scope of T, generic inheritance,
 public class ParentClass 
{

}
Public class ChildAClass: ParentClass
{

}
Public class ChildBClass: ParentClass
{

}

Public class ClassTest T: where ParentClass
{
Public T GetChild (ParentClass PC, string classType)
{
Return PC (T);
}
}

CodePudding user response:

refer to the second floor bloodish response:
I guess you want to get a subclass by classType Type (Type)? Because if just type conversion, you don't need to write like this, directly (PC as T)
Don't need a paradigm, do not need to pass the superclass object
 
Public static Type GetChild (string classType)
=> The Assembly
GetExecutingAssembly ()
The GetTypes ()
The Where (t=& gt; Typeof (ParentClass). IsAssignableFrom (t) & amp; & T.N ame==classType)
FirstOrDefault ();
hello, thank you for your help, I want to return is not a Type, but a specific subclass types, for example, ChildAClass; Parameter is the parent class, and the specific subclass type or types of string;

CodePudding user response:

refer to the third floor is nu month god reply:
since want to convert, that is about to define the scope of T, generic inheritance,
 public class ParentClass 
{

}
Public class ChildAClass: ParentClass
{

}
Public class ChildBClass: ParentClass
{

}

Public class ClassTest T: where ParentClass
{
Public T GetChild (ParentClass PC, string classType)
{
Return PC (T);
}
}
hello, thank you for your reply, you may not know what I mean

CodePudding user response:

reference 1/f, the knights templar 18 response:
this writing don't you call a generic, generic is characterized by: specified type at runtime, you only have one T, the return value is that there is no specified type "" ah, you write the overall framework of has a problem, not just fill in the blanks of the code of how to write, to change the framework code,
how do you do, you may not understand what I mean, if need to change frame I would not have to initiate the problem

CodePudding user response:

Is not we have a question, do you have any problem, first you have to admit that he put forward the problem of wrong
1, first of all, you this question, has nothing to do with generics, generic thing, you say that you don't understand generics,
2, according to the fun (parent class type, the subclass type string), can't get a subclass type, it is mathematically impossible, that means you are for the thinking of father and son class type is chaotic,

, for example, suppose that you don't have a wife, also had no sons, but think about you, and then give your son a name, called "king" five, and then you ask solution:
Var wang five type=fun (typeof (for you), "wang five"),
How would it be possible to get wang five types? Wang five also does not exist in this world,

To get a class type, only according to the position of the binary code to get it, such as:
Var wang five Type=Type. GetType (" in the womb. Your future wife. A fertilized egg "); , it doesnt need the type of "you" as a transport to, also don't have to,

Finally, give you a piece of code, see how to obtain a type c #
Var type=type. GetType (" Kernel. SimpleLibrary. PlugPut, Kernel. SimpleLibrary ");
  •  Tags:  
  • C#
  • Related