JokerMartini February 2016

C# distinguishing incoming objects using attribute

I need a way of distinguishing class objects which a user is passing into my custom control. The reason for this, is so I can handle certain logic on the back end, completely extracted and uncontrollable by the user. The user would pass in object collections like this...

<Grid>
    <nexus:NexusEditor 
        SourceTeams="{Binding Teams}"
        SourcePlayers="{Binding Players}"
        SourceLocations="{Binding Locations}"/>
</Grid>

If i were to forloop through all these objects in my custom control, I need a way of marking the incoming objects so i know which Source there were passed through.

Answers


Jaco February 2016

This can't be easily done at runtime and is arguably the wrong design in C#. There are other ways to solve this. You can, for example, create a wrapper around the Connection class and inject an instance of Connection into it. ConnectionWrapper can then inherit AbstractBase

public class ConnectionWrapper: AbstractBase
{
   public ConnectionWrapper(Connection connnection)
   {

   }

   private override int _type {get; set;}
}


kai February 2016

It's really hard to give something concrete without knowing more about your requirements, but I think something like this might do what you want:

public interface ITyped
{
    int Type { get; }
}

public class Connection : ITyped
{
    public string Name { get; set; } // NOTE: public setters are Bad Code(tm) for anything but the dumbest DTO objects

    public int Type { get { return 1; } } // specify type for connections here
}

Usage:

foreach (var typed in connections.Concat<ITyped>(groups))
{
    DoStuffWith(typed);
}

Post Status

Asked in February 2016
Viewed 2,254 times
Voted 6
Answered 2 times

Search




Leave an answer