简体   繁体   English

是否可以在C#中将类型设置为超类uf的变量?

[英]Is it possible to set a Variable with a Type which is a superclass uf a class in C#?

More Informations: 更多信息:

EDIT: better sample: 编辑:更好的示例:

I have a class in a Libary called UserAccount, its abstact. 我在一个库中有一个名为UserAccount的类,它的摘要。 then i have some functionality like this in the library: 然后我在库中有一些这样的功能:

class UserAccountService
{
    public static UserAccount CreateUserAccount(String username, String password, String email)
    {
    UserAccount account = new UserAccount();
    account.Username = username;
    account.HashedPass = //Some crypting stuff with password
    account.Email = email;

    UserAccountRepository db = new UserAccountRepository();
    db.UserAccounts.Add(account);

    return account;
    }
}

Cuz this is a independent library the UserAccount has not all Propertys i want to use: 因为这是一个独立的库,UserAccount没有我要使用的所有属性:

class ExtendedUserAccount : UserAccount
{
// define some additional methods and propertys
public Contact Contacts{get;set}// this property is only used in one application where i use the Library....
}

Then i want to do this: 然后我要这样做:

ExtendedUserAccount newAccount = UserAccountService.CreateUserAccount(new UserAccount);

But this will not work. 但这是行不通的。 i now its not correct but i need something similar... 我现在不正确,但我需要类似的东西...

anyone has an idea?? 有人有主意吗?

That looks like a code smell and you probably need to redesign your types... But anyway, this should work: 看起来像是代码的味道,您可能需要重新设计类型。但是无论如何,这应该可以工作:

class UserAccountService
{
    public static TAccount CreateUserAccount<TAccount>(TAccount account)
          where TAccount : UserAccount, new()
    {
        //create new useraccount...
        return account;
    }
}

This generic method takes an instance of a type that must extend UserAccount (or be an UserAccount itself), and declare a parameterless constructor. 此通用方法采用必须扩展UserAccount(或本身为UserAccount)的类型的实例,并声明无参数构造函数。 This last restriction will allow you to do this: TAccount account = new TAccount() . 最后一个限制将使您可以执行此操作: TAccount account = new TAccount()

I recommand the factory pattern 我建议工厂模式

class UserAccountService
{
    // ctor with interfaces
    public IUserAccount CreateUserAccount()
    {
        // create instance
        return result;
    } 

    // ctor with generic
    public IUserAccount CreateUserAccount<T>() where T : UserAccount
    {
        var account = Activator.CreateInstance<T>();
        return account;
    } 
}

class ExtendedUserAccount : UserAccount
{
    // define some additional methods and propertys
}

class UserAccount : IUserAccount
{

}

internal interface IUserAccount
{
}

Just to make it clear 只是为了说清楚

Are this you conditions? 这是您的条件吗?

  • UserAccountService & UserAccount are in Library A UserAccountService和UserAccount在库A中
  • ExtendedUserAccount is, and only will be in Library B 现为ExtendedUserAccount,并且只会在库B中
  • you can't/dont't want to edit library A 您不能/不想编辑库A

then the answer can be: Make 1 entry point in Library B that make this possible 那么答案可能是:在库B中创建1个入口点使之成为可能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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