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;
精彩评论