决定在运行时使用不同的属性实例化类 [英] Decide to instantiate classes at run time with different properties

查看:73
本文介绍了决定在运行时使用不同的属性实例化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三节课



I have three classes

class Student
   {
       public string Name { get; set; }
       public int ID { get; set; }
       public int RollNumber { get; set; }
   }

   class Room
   {
       public int Length { get; set; }
       public int Breadth { get; set; }
       public int Height { get; set; }
   }

   class Account
   {
       public string AccountholderName { get; set; }
       public int AccountID { get; set; }
       public int Amount { get; set; }
   }





在运行时我想在某种情况下返回一个类的实例。就像工厂模式一样。我不能使用工厂模式,因为类没有相同的属性,当我们实例化接口时因为实例化接口我可以获得接口属性而不是类属性。建议写入方法或任何其他模式。



[来自评论]



At runtime i would like to return instance of one class at some condition. Just like factory pattern.I can not use factory pattern because classes does not have same properties and when we instantiate Interface because when instantiate interface i can get interface properties not that class properties. Suggest write approach or any other pattern.

[From comment]

interface InterfaceToImplement 
{
    string Name { get; set; }
}
 
// Implementing Classes
class Student : InterfaceToImplement 
{
    public string Name { get; set; }
    public int ID { get; set; }
    public int RollNumber { get; set; }
}
 
class Room : InterfaceToImplement 
{
    public string Name { get; set; }
    public int Length { get; set; }
    public int Breadth { get; set; }
    public int Height { get; set; }
}
 
class Account : InterfaceToImplement 
{
    public string Name { get; set; }
    public string AccountholderName { get; set; }
    public int AccountID { get; set; }
    public int Amount { get; set; }
}
 

// Factory Class
class ClassFactory
{
    public InterfaceToImplement GetClass(string classname)
    {
        InterfaceToImplement ObjectClass = null;
        switch (classname)
        {
            case "Student":
                ObjectClass = new Student();
                break;
            default:
                break;
        }
         
        return ObjectClass;
    }
}
         
// Calling Method
static void Main(string[] args)
{
    string classname = "Student";
    ClassFactory clsfactory = new ClassFactory();
    InterfaceToImplement GetObjIntrface = null;
    GetObjIntrface = clsfactory.GetClass(classname);    
}





当我使用GetObjIntrface时。然后我只使用接口的属性但我想要类Student的所有属性。



When I use GetObjIntrface. Then i use only properties of interface but i want all the properties of class Student.

推荐答案

我不知道到底是什么,但你可以试试,



I am not sure what are up to, but you may try,

internal class ClassFactory
{
    public TSource GetClass<tsource>()
        where TSource : class, InterfaceToImplement, new()
    {
        return new TSource();
    }
}

ClassFactory clsfactory = new ClassFactory();
var student = clsfactory.GetClass<student>();
var room = clsfactory.GetClass<account>();
var account = clsfactory.GetClass<account>();





在网上看看泛型如何用于c#



take a look in web how generics works for c#


这篇关于决定在运行时使用不同的属性实例化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆