Is there anyway to create such interface which generates properties as generic.
public interface IInterface<T>
{
string nameOf(T)+"_Email" { get; set; } // this won`t compile
string nameOf(T)+"_Phone" { get; set; } // this won`t compile
}
public class Person
{
}
public class Details : IInterface<Person>
{
public string Person_Email { get; set; }
public string Person_Phone { get; set; }
}
I asked above question because my problem was as follow. I want to secure two classes with Interface contract. Then I combine these two classes in a ViewModel. Viewmodel is not really helping because I need these properties on Razor. Please see below.
public interface IPerson
{
string Email { get; set; }
string Phone { get; set; }
}
public interface IHotel
{
string Email { get; set; }
string Phone { get; set; }
}
public class Person : IPerson
{
public string Email { get; set; }
public string Phone { get; set; }
}
public class Hotel: IHotel
{
public string Email { get; set; }
public string Phone { get; set; }
}
public class ViewModel1 : IPerson, IHotel
{
//
// This is missing either Person or Hotel details
//
public string Email { get ; set ; }
public string Phone { get ; set ; }
}
public class ViewModel2 : IPerson, IHotel
{
//
// This is ok but PUBLIC modifier is not allowed, I cannot use.
//
string IPerson.Email { get ; set ; } // public modifier not allowed
string IHotel.Email { get ; set ; } // public modifier not allowed
string IPerson.Phone { get ; set ; } // public modifier not allowed
string IHotel.Phone { get ; set ; } // public modifier not allowed
}
No. It is not possible to dynamically modify the names of interface members with a class level generic argument.
Generics are designed to enable you to re-use the same functionality regardless of which generic type is specified. This is only possible if the interface remains consistent.
Consider this dilemma for example:
public class Foo<T>
{
public string GetPhone(IInterface<T> bar)
{
// how would I know what method to call on foo here?
return bar.????_Phone;
}
}
Below is an example of how you can have generic properties in an interface. You need a base class to translate for you and to bind the type you are looking for
public interface IInterface<T> where T : Contact
{
string Email { get; }
string Phone { get; }
}
public class Person : Contact
{
public string Phone { get; set; }
public string Email { get; set; }
}
public class DetailsBase<T> : IInterface<T> where T : Contact
{
Contact _cont { get; set; }
public string Email { get { return _cont.Email; } }
public string Phone { get { return _cont.Phone; } }
public DetailsBase(Contact cont)
{
_cont = cont;
}
}
public class Contact
{
public string Email { get; set; }
public string Phone { get; set; }
}
public class PersonDetails : DetailsBase<Person>
{
public PersonDetails(Person person) : base(person)
{
}
}
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.