One option for such problems is to apply an adapter pattern to your classes. In this case, they have the same properties, but not the same interface. (This is most useful if you do not control the source code or otherwise it makes no sense for them to actually use the interface in normal scripts.)
interface ILinkablePath { string LinkPath { get; } } class LinkBaseAdapter : ILinkablePath { private LinkBase linkBase; public LinkBaseAdapter(LinkBase linkBase) { this.linkBase = linkBase; } public string LinkPath { get { return this.linkBase.LinkPath; } } }
You can write an adapter for each class that you want to support. If they donβt have a common interface and may not even have common methods or properties (in the name, if not in execution), you can use this method if you want to use classes in the same way.
When you do this, you can fully work with the adaptation interface in your methods.
Otherwise, for your specific problem, you can simply create different methods for each type that you want to support, or (if you just work with a single property, and not with the rest of the object), just go to the value of the property you want to use, not the entire object.
Anthony pegram
source share