Reputation: 8171
I have this interface
public interface IMyInterface
{
IEnumerable<MyParamInfo> Params { get; }
}
where MyParamInfo is
public class MyParamInfo
{
public MyParamInfo (string name)
{
Name= name;
}
public string Name { get; private set; }
}
also this class
public class MyClass:IMyInterface
{
//properties
....
public IEnumerable<MyParamInfo> Params
{
get
{
return new List<MyParamInfo> { new MyParamInfo("Param1")};
}
}
}
and this Form
public partial class MyForm<T> : Form where T:Class,IMyInterface
{
...
}
with this code
MyForm<MyClass> frm = new MyForm<MyClass>();
How can I access to Params Property of MyClass in frm object?
Upvotes: 2
Views: 574
Reputation: 16324
If you also require that the T
type parameter of MyForm
have a parameterless constructor, you can instantiate an instance of T
and then use the interface property at will.
On the definition of MyForm
, add the new()
generic constraint
public partial class MyForm<T> : Form where T : Class, IMyInterface, new()
Then in some method of MyForm<T>
, you can use:
(new T()).Params;
You can read about all the constraints on type parameters in C# here.
It seems like what you really want is interfaces that could specify static methods (so-called static interfaces). Such a construct does not exist in C#.
Upvotes: 3