第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

C# 中的參數(shù)化 System.Type 變量

C# 中的參數(shù)化 System.Type 變量

C#
翻翻過去那場(chǎng)雪 2022-06-19 16:48:03
我正在嘗試在 C# 中創(chuàng)建一個(gè)工廠類,該類返回屬于或擴(kuò)展某個(gè)基本類型的對(duì)象。每次調(diào)用getInstance()工廠時(shí)我都需要實(shí)例化這種類型的新實(shí)例,所以我真的只想接受并存儲(chǔ)類型本身。在 Java 中,我使用Class<? extends Base>來保存要?jiǎng)?chuàng)建的類,然后調(diào)用getInstance()它。我了解如何使用 C# 中的 Activator 類從 a 創(chuàng)建新對(duì)象,System.Type但我不確定的部分是對(duì)類類型的約束。我希望能夠只接受屬于或擴(kuò)展基類的類型。我意識(shí)到我可以更改工廠的 setter 以接受基本類型的實(shí)際對(duì)象,然后從中檢索類型,但我真的不想實(shí)例化整個(gè)對(duì)象只是為了檢索 Type 變量。下面是一個(gè)小示例 Java 程序,只是為了演示我需要什么,以防我的問題不清楚。有沒有辦法在 C# 中做到這一點(diǎn)?class Program {   public static void main(String[] args) throws InstantiationException, IllegalAccessException {         Factory.setClass(Base.class);         Base b = Factory.getInstance();         b.Print();         Factory.setClass(Child.class);         b = Factory.getInstance();         b.Print();      }}class Factory {   private static Class<? extends Base> type;   // What I want to do in C#.   public static void setClass(Class<? extends Base> newType) {      type = newType;   }   // What I don't want to have to do in C#.   public static void setClassFromObject(Base newTypeObject) {      type = newTypeObject.getClass();   }   public static Base getInstance() throws InstantiationException, IllegalAccessException {      return type.newInstance();   }}class Base {   public void Print() {      System.out.println("Hello from base.");   }}class Child extends Base {   @Override   public void Print() {      System.out.println("Hello from child.");   }}
查看完整描述

2 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

我看不到如何在編譯時(shí)強(qiáng)制執(zhí)行此操作,但如果您對(duì)運(yùn)行時(shí)檢查沒問題,您可以這樣做:


class Factory 

{

    private static Type _type;


    public static void SetClass(Type t) 

    {

        if (!(typeof(Base)).IsAssignableFrom(t))

        {

            throw new ArgumentException("type does not extend Base", nameof(t));

        }


        _type = t;

    }


    public static Base GetInstance() 

    {

        return (Base)Activator.CreateInstance(_type);

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-06-19
?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以使“GetInstance”方法動(dòng)態(tài)化,以便在設(shè)置類時(shí)也設(shè)置方法。這樣你就可以在運(yùn)行時(shí)依賴泛型來獲得正確的類型。它可能看起來像這樣:


public class Factory

{

    private static Func<Base> _getInstance;


    //option if you want to pass in an instantiated value

    public static void SetClass<T>(T newType) where T : Base, new()

    {

        _getInstance = () => new T();

    }


    //option if you just want to give it a type

    public static void SetClass<T>() where T : Base, new()

    {

        _getInstance = () => new T();

    }


    public static Base GetInstance()

    {

        return _getInstance();

    }


    //you could just make GetInstance Generic as well, so you don't have to set the class first

    public static Base GetInstance<T>() where T : Base, new()

    {

        return new T();

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-06-19
  • 2 回答
  • 0 關(guān)注
  • 151 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)