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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據(jù)通用接口獲取類屬性

根據(jù)通用接口獲取類屬性

C#
泛舟湖上清波郎朗 2021-06-23 17:01:16
可以說我有這個:public interface IMyInterface<T>{}public class MyClass{    public IMyInterface<Foo> Foos {get; set;}    public IMyInterface<Bar> Bars {get; set;}}我想要一個這樣的方法MyClass.Interfaces<T>()哪個將返回 MyClass.Foos 或 MyClass.Bars 取決于 T 值。我怎么做?這與 EF 的工作方式非常相似。
查看完整描述

2 回答

?
函數(shù)式編程

TA貢獻1807條經(jīng)驗 獲得超9個贊

它需要一些管道和鑄造,但你可以用字典來做到這一點:


public interface IMyInterface<T> {

}

public class Foo { }

public class Bar { }


public class MyClass {


    Dictionary<Type, object> myInterfaces = new Dictionary<Type, object>();


    public IMyInterface<Foo> Foos {

        get { return (IMyInterface<Foo>)myInterfaces[typeof(Foo)]; }

        set { myInterfaces[typeof(Foo)] = value; }

    }

    public IMyInterface<Bar> Bars {

        get { return (IMyInterface<Bar>)myInterfaces[typeof(Bar)]; }

        set { myInterfaces[typeof(Bar)] = value; }

    }


    public IMyInterface<T> Interfaces<T>() {

        return (IMyInterface<T>)myInterfaces[typeof(T)];

    }


}


查看完整回答
反對 回復(fù) 2021-06-26
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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