Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How do you return a Type based off of a string

Tags:

c#

.net

For example, I have a class called Clothing that inherits from a abstract class of Product.

public class Clothing : Product
{
    public bool IsInSeason {get; set;}
    public string Material {get; set; }
    public Decimal IsFeatured { get; set; }
}

Edit: In a function, how would I be able to pass the string "Clothing" and have a typeof the class returned?

ReturnTypeOfClassBasedOffOfString(string class)
{
    // ... 
}
like image 746
contactmatt Avatar asked Dec 09 '25 20:12

contactmatt


1 Answers

Use Type.GetType(string)

Note that sometime the class name won't be enough. Sometime you will need to provide the AssemblyQualifiedName

"If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace."

like image 194
Jean-Philippe Leclerc Avatar answered Dec 12 '25 09:12

Jean-Philippe Leclerc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!