Reputation: 2503
In a project I'm working has a class defined like this:
class DataImport<T> : DataBaseClass where T : IDataEntity, new()
{
}
The IDataEntity
has fields like this:
string tableName { get; }
string Id { get; }
int TypeId { get; }
In a class that implements this interface there is this:
string IDataEntity.Id { get { return myobject_id.ToString(); } }
int IDataEntity.TypeId { get { return 10; } }
string IDataEntity.tableName { get { return "tblObjects"; } }
For example.
In the DataImport
object I would like to do this:
string x = (T).tableName;
etc. But of course I can't do that. I've tried declaring them as public but it's not allowed.
How would I access the tableName
, Id
and TypeId
properties? I've looked at typeof(T).GetProperties()
but they are null
; in GetFields()
also.
Upvotes: 0
Views: 467
Reputation: 43876
There are two issues:
T
to access the non-static property tableName
, in your example you call (T).tableName
on the type instead of an instance.string IDataEntity.tableName
) you will need to cast the instance of T
into an IDataEntity
explicitly.So your DataImport
implementation should look like this:
class DataImport<T> : DataBaseClass where T : IDataEntity, new()
{
public void WriteTableName(T arg)
{
string x = ((IDataEntity)arg).tableName;
Console.WriteLine(x);
}
}
Upvotes: 4