-2

I have base class Entity and an enheritance class say Home ,

public class Home : Entity
{
    public int CityId{get;set;}
}

public class Town : Entity 
{
    public int  CityId {get;set}
    public Home CityHall {get;set;}
    public List<Home > Homes{get;set;}
}

I want to set the CityId for Town and its children so a first try I did the following

public class DataAccessBase<T> where T : Entity
{
    public int Add(T entity)
    {
        Type t = typeof(T);
        PropertyInfo prop = t.GetProperty("CityId");
        if (prop != null)
        {
            prop.SetValue(entity, 2);
        }
    }
}

this work only for the parent how to access children , I want to d othat generically simply because I have a dataaaccesslayer that insert of Database genrically

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
AMH
  • 6,363
  • 27
  • 84
  • 135
  • 1
    You can set the properties directly no need for reflection – Nkosi Apr 28 '16 at 16:00
  • No I have a generic class of adding to database so am in need t ogenerically do that @Nkosi – AMH Apr 28 '16 at 16:00
  • None of your classes are generic? Also, CityId is not a property of type Type?? – monstertjie_za Apr 28 '16 at 16:01
  • 3
    Side note: I'd strongly recommend to not use word "generic" for regular classes to avoid confusion with generics... – Alexei Levenkov Apr 28 '16 at 16:01
  • Your `CityID`property doesn't have a type. – AntiTcb Apr 28 '16 at 16:03
  • @AlexGravely See my updated question – AMH Apr 28 '16 at 16:04
  • @AlexeiLevenkov I updated my question – AMH Apr 28 '16 at 16:05
  • 1
    You need to show `Entity`. You question is not clear. You need to explain it better. – Nkosi Apr 28 '16 at 16:08
  • 3
    Your question is still very confusing. You say you don't know the incoming type, but you assume that it has a `CityId` property that's assignable from to an int. It's not at all clear why this method is actually 'generic'; for example it seems far easier to have separate `Add(Home)` and `Add(Town)` methods (or some other non-generic implementation). How are you trying to call this method? – p.s.w.g Apr 28 '16 at 16:10

2 Answers2

2

It looks like there are two unrelated problems

  • how to set property of an object without knowledge if property is there: reflection as you have it solve that. Note that this is not very C# way - you'd use some interface and restrict generics to that interface to allow strongly typed access to properties.

  • how to enumerate "child" objects without knowing type: traditional solution again is to add interface for "GetChildren" functionality. Alternatively you can go with reflection and find all properties that are of "child" type and combine with all properties that are of type IEnumerable<"child type">.

    If you can use some convention dynamic could be easier alternative to reflection (i.e. every type exposes Children property to enumerate them:

    dynamic town = GetTown();
    foreach(dynamic child in town.Children) {...}
    
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 2
    FWIW, I like the interface solution a lot more than `dynamic`. E.g. `interface IHasChildren where T : Entity { IEnumerable GetChildren(); }` then `Town : Entity, IHasChildren { ... }`. However, I don't think the question is clear enough to answer completely. – p.s.w.g Apr 28 '16 at 16:19
  • @p.s.w.g indeed - I'd use `dynamic` only as alternative to reflection (i.e. if code can't be changed to add common interface) – Alexei Levenkov Apr 28 '16 at 16:40
  • 2
    I'm not sure if you're aware of this, but just to be clear for other readers, `dynamic` is actually just syntactic sugar for plain old reflection. The C# compiler will convert this to something roughly like `(IEnumerable)(town.GetType().GetProperty("Children").GetValue(town))`. – p.s.w.g Apr 28 '16 at 17:05
1

You can set the properties directly no need for reflection.

entity.CityId = 1;
if(entity is Town) {
    var town = entity as Town;
    if(town.Homes!=null) {
        town.Homes.ForEach(t=> t.CityId = entity.CityId);
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • this generic classes is a cconcept I don;''t know the comming type or its children – AMH Apr 28 '16 at 16:06