开发者

Multiple Interfaces - Just need 1's ID

开发者 https://www.devze.com 2023-03-09 04:32 出处:网络
I have a dynamic collecti开发者_JAVA百科on of Interfaces in c# the collection is loose - not bound to a List / Dictionary / etc.

I have a dynamic collecti开发者_JAVA百科on of Interfaces in c# the collection is loose - not bound to a List / Dictionary / etc.

but at any given time does only 1 of the interfaces have a valid nested class "ID" (Guid)

so, to avoid to the repetative task of duplicating all conditions jsut to validate the actual id. Is there a quicker / efficient way of determining the ID from a single interface ?

for instance:

ICookies;
IBread;
IJar;

if (ICookies.BaseObject.ID != null)
   this.ID = ICookies.BaseObject.ID

if (IBread.BaseObject.ID != null)
   this.ID = IBread.BaseObject.ID

if (IJar.BaseObject.ID != null)
   this.ID = IJar.BaseObject.ID

.... etc. etc.


You could simplify your code, like:

this.ID = ICookies.BaseObject.ID ?? IBread.BaseObject.ID ?? IJar.BaseObject.ID;
0

精彩评论

暂无评论...
验证码 换一张
取 消