リフレクションを使用して次の C# コードを複製しようとしています。
UserProfileManager userProfileManager = new UserProfileManager(ServerContextGoesHere);
UserProfile userProfile = null;
userProfile = userProfileManager.GetUserProfile(@"somedomain\someuser");
userProfile["PictureUrl"].Value = "This is where I want to update the value using reflection!";
userProfile.Commit();
リフレクションを使用すると、UserProfile オブジェクトの "PictureUrl" インデックス付きプロパティを設定しようとしている行を除いて、すべてを機能させることができます。逆コンパイラを使用すると、そのインデックス付きプロパティは次のようになります。
public UserProfileValueCollection this[string strPropName]
上記と同じことを達成するためにリフレクションを使用するコードを次に示します。PictureUrl インデックス付きプロパティの値を設定する必要がある TODO コメントに注目してください。
Assembly userProfileAssembly;
var windowsFolderPath = Environment.GetEnvironmentVariable("windir");
var pathToServerAssembly = string.Format(@"{0}\assembly\GAC_MSIL\Microsoft.Office.Server.UserProfiles\14.0.0.0__71e9bce111e9429c\Microsoft.Office.Server.UserProfiles.dll", windowsFolderPath);
try
{
userProfileAssembly = Assembly.LoadFrom(pathToServerAssembly);
}
catch (FileNotFoundException)
{
// Assembly wasn't found, so eject.
return;
}
var userProfileManagerClass = userProfileAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfileManager");
if (userProfileManagerClass == null) return;
var userExistsMethod = userProfileManagerClass.GetMethod("UserExists");
if (userExistsMethod == null) return;
var getUserProfileMethod = userProfileManagerClass.GetMethod("GetUserProfile", new[]{typeof(string)});
if (getUserProfileMethod == null) return;
var instantiatedUserProfileManagerClass = Activator.CreateInstance(userProfileManagerClass);
var result = (bool)userExistsMethod.Invoke(instantiatedUserProfileManagerClass, new object[] { SPContext.Current.Web.CurrentUser.LoginName });
if (!result) return;
var userProfileClass = userProfileAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfile");
var userProfile = getUserProfileMethod.Invoke(instantiatedUserProfileManagerClass, new object[] { SPContext.Current.Web.CurrentUser.LoginName });
//userProfile["PictureUrl"].Value = userPictureUrl;
//TODO: HOW DO I SET THE PICTUREURL PROPERTY USING REFLECTION?
var commitMethod = userProfileClass.GetMethod("Commit");
commitMethod.Invoke(userProfile, null);
前もって感謝します、
ライアン