简体   繁体   中英

Using Abstract Classes in C#

I have an abstract class that other classes are inheriting this.

public abstract class GenericAccess<TEntity>
{
    public static IList<TEntity> GetObjectListFromArray(int[] IDs)
    {
        //Your help need is here.
    }
}

public class Doors : GenericAccess<DoorEntity>
{

}

public class DoorEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I need to create a generic method so when I can for example

IList<DoorEntity> Doors.GetObjectListFromArray(int[] {1,2,3,4,5});

it will return an IList having all the objects in it with the property Id loaded with the value passed. In the above example it will return a list with 5 items in the list with the Id of DoorEntity loaded.

Use an interface or a base class...

With interface:

public abstract class GenericAccess<TEntity> where TEntity : IEntity, new()
{
    public static IList<TEntity> GetObjectListFromArray(int[] IDs)
    {
        return IDs.Select(id => new TEntity { Id = id }).ToList();
    }
}

public class Doors : GenericAccess<DoorEntity>
{

}

public class DoorEntity : IEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public interface IEntity
{
    int Id { get; set; }
    string Name { get; set; }
}

With a base class:

public abstract class GenericAccess<TEntity> where TEntity : Entity, new()
{
    public static IList<TEntity> GetObjectListFromArray(int[] IDs)
    {
        return IDs.Select(id => new TEntity { Id = id }).ToList();
    }
}

public class Doors : GenericAccess<DoorEntity>
{

}

public class DoorEntity : Entity
{

}

public abstract class Entity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I'm not sure if that's what you want but here goes:

return from int id in Ids
       select new TEntity (id);

You'll have to correct the definition of the GenericAccess class to add a constraint to the generic parameter as follow:

public abstract class GenericAccess<TEntity> where TEntity : class, new

Ok, based on your comments...

Use LINQ with NHibernate to get the entities something along the lines of:

return from int id in Ids
       select Session.Query (...).Where (x => x.Id === id);

your function can be like this

public static IList<TEntity> GetObjectListFromArray(int[] IDs)
{
    var r = new List<TEntity>();
    foreach (var item in IDs)
    {
        var obj = typeof(TEntity).Assembly.CreateInstance(typeof(TEntity).FullName);
        var p = obj.GetType().GetProperty("Id", System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
        if (p != null)
        {
            p.SetValue(obj, item, null);
            var m = r.GetType().GetMethod("Add");
            m.Invoke(r, new object[] { obj });
        }
    }
    return r;
}

}

and

    IList<DoorEntity> r = Doors.GetObjectListFromArray(new int[] {1,2,3,4,5});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM