c# - Is there a handy way to figure out whether an Object implements -
i'm trying figure out if there builtin routines or classes in .net framework provide convenient way test whether collection implements among following interfaces , hence hook relevant events, if any:
ienumerable
ilist
icollection
ibindinglist
ienumerable <t>
ilist <t>
icollection <t>
iraiseitemchangedevents
inotifycollectionchanged
mostly data-binding purposes...
i can go lot of reflections (e.g. isassignablefrom) since seems pretty common scenario, wondering if there done in regard.
[edit]
seems question vague or poorly phrased, bad. indicated bit later in 1 of comments more looking way provide centralized databinding way of doing, interested make ui-agnostic, seems ms provides internally tooling such as: http://referencesource.microsoft.com/#system.windows.forms/winforms/managed/system/winforms/datagridviewdataconnection.cs,68950bc360ed4e45,references nothing public.
right now, approach wrap kind of bindingsource equivalent , check whether object passed constructor implements of interface willing hook with.
you alternatively is
operator of previous answer use as
operator:
var ienumerable myienumerableobject = unknownobject ienumerable; if(myienumerableobject != null) { myienumerableobject.someevent += someeventhandler; }
Comments
Post a Comment