[英]Cannot implicitly convert system.directoryservice.accountmanager.userprinciple to extended userprincipal
我創建了一個類和派生類。 如何調用另一個類的派生類? 請參見下面的類,派生類(擴展類)以及我稱之為派生類的地方。
UserPrincipal
類:
private UserPrincipal GetUserFromAD1(string userLoginName)
{
String currentUser = userLoginName;
String domainName = userLoginName.Split('\\')[0];
UserPrincipal user = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName))
{
user = UserPrincipal.FindByIdentity(pc, System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, currentUser);
}
});
return user;
}
擴展的UserPrincipal
類:
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
public class UserPrincipalExtended : UserPrincipal
{
public UserPrincipalExtended(PrincipalContext context): base(context)
{
}
[DirectoryProperty("department")]
public string department
{
get
{
if (ExtensionGet("department").Length != 1)
return null;
return (string)ExtensionGet("department")[0];
}
set { this.ExtensionSet("department", value); }
}
方法
private void GetUserInfoFromAD1()
{
try
{
XPathNavigator rootNode = this.MainDataSource.CreateNavigator();
this.initialData = rootNode.SelectSingleNode("/my:myFields/my:Wrapper", this.NamespaceManager).OuterXml;
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
UserPrincipalExtended userFromAD = this.GetUserFromAD1(web.CurrentUser.LoginName);
在代碼行上
this.GetUserFromAD1(web.CurrentUser.LoginName)
我收到一個錯誤:
無法隱式轉換類型.......
你似乎是試圖分配的實例UserPrincipal
從返回GetUserFromAD1
到類型的變量UserPrincipalExtended
。
那不是可以隱式執行的類型轉換。
您可以將類型的實例隱式分配給它的超類型(或其超類型或它實現的接口)。
您不能將類型的實例隱式分配給該類型的子類型。
請考慮執行department
作為擴展方法UserPrincipal
或作出UserPrincipalExtended
的一個實例的包裝UserPrincipal
,或提供一個轉換運算投UserPrincipal
到UserPrincipalExtended
,或使用拷貝構造函數。
有許多設計可用於此目的。 您寫的不會。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.