-
Hi, I hit the following limitation for initializing an explicitly implemented interface init property. public class UserRoleEntity : IUserRole
{
public UserRoleEntity(IUser user, IRole role)
{
((IUserRole)this).User = user; // this here does not work, described above *.
this.Role = role; // this here does not work because IRole not RoleEntity.
}
public UserEntity User { get; init; }
public RoleEntity Role { get; init; }
IUser IUserRole.User { get; init; }
IRole IUserRole.Role { get; init; }
}
public interface IUserRole
{
IUser User { get; init; }
IRole Role { get; init; }
} Is there a possibility to initialize the property? * Init-only property or indexer can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Side question, is it really preferable to allow the explicitly implemented properties to be set to something different than the public properties? I would have anticipated seeing this: public UserEntity User { get; init; }
public RoleEntity Role { get; init; }
IUser IUserRole.User => User;
IRole IUserRole.Role => Role; |
Beta Was this translation helpful? Give feedback.
-
We explicitly looked at this, and rejected the feature: https://github.com/dotnet/csharplang/blob/master/meetings/2021/LDM-2021-01-27.md#init-only-access-on-conversion-on-this. |
Beta Was this translation helpful? Give feedback.
We explicitly looked at this, and rejected the feature: https://github.com/dotnet/csharplang/blob/master/meetings/2021/LDM-2021-01-27.md#init-only-access-on-conversion-on-this.