我遇到了问题。我有一个基于Randy Patterson流畅的接口示例的子类:
Randy Patterson's Fluent Interface Design Page
我正在继承User类,需要将其中设置的属性传递给UserFluentInterface子类中的下面的CreateNewUser方法。
public void CreateNewUser()
{
try
{
User user = (User)_User;
MembershipCreateStatus status;
MembershipUser memuser = _membership.CreateUser(user, out status);
}
catch (Exception e)
{
string message = e.Message;
}
}
和_membership.CreateUser具有签名:
public MembershipUser CreateUser(User user, out MembershipCreateStatus status);
我收到错误:
无法访问非静态成员 外部类型'MyProject.Schema.User'通过 嵌套类型 'MyProject.Schema.User.UserFluentInterface'
我不确定如何解决这个问题。代码如下:
User user = _someAPI.GetUserProfile(txtEmail.Text);
user.Set
.Email(txtEmail.Text)
.Password(txtPassword.Text)
.CreateNewUser();
现在,我有一个带有子类的User类:
public class User : ProfileBase, ISessionMgrEntry
{
MyMembershipProvider _membership = null;
UserFluentInterface _set = null;
public User()
{
_membership = (MyMembershipProvider)Membership.Providers["MyMembershipProvider"];
_set = new UserFluentInterface(this);
}
public UserFluentInterface Set
{
get { return _set; }
}
#region ProfileBase Members
[SettingsAllowAnonymous(false), CustomProviderData("Email;string")]
public string Email { get { return base["Email"] as string; } set { base["Email"] = value; } }
[SettingsAllowAnonymous(false), CustomProviderData("Password;string")]
public string Password { get { return base["Password"] as string; } set { base["Email"] = value; } }
public class UserFluentInterface
{
private readonly User _User;
public UserFluentInterface(User User)
{
_User = User;
}
public UserFluentInterface Email(string email)
{
_User.Email = email;
return this;
}
public UserFluentInterface Password(string password)
{
_User.Password = password;
return this;
}
public void CreateNewUser()
{
try
{
User user = ???? // HOW DO I ACCESS THE PROPERTIES THAT WERE ORIGINALLY SET AND PASS THEM AS AN OBJECT BELOW?
MembershipUser memuser = _membership.CreateUser(user);
}
catch (Exception e)
{
string message = e.Message;
}
}
}
}
答案 0 :(得分:0)
如果您不需要User user = _User
的副本,则可以写_User
。
否则,复制_User
并传递它。
答案 1 :(得分:0)
我认为您必须将用户和成员资格都传递到UserFluentInterface:
public class UserFluentInterface
{
private readonly User _User;
private readonly Membership _Membership;
public UserFluentInterface(User User, Membership membership)
{
_User = User;
_Membership = membership;
}
}
然后使用_Membership而不是_membership。在您的CreateNewUser()方法