gpt4 book ai didi

c# - 使用反射设置索引属性的值

转载 作者:行者123 更新时间:2023-11-30 21:03:31 25 4
gpt4 key购买 nike

我正在尝试使用反射复制以下 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);

提前致谢

瑞安

最佳答案

假设您在 UserProfile 上只有一个索引器:

PropertyInfo indexProperty = typeof(UserProfile)
.GetProperties()
.Single(p => p.GetIndexParameters().Length == 1 && p.GetIndexParameters()[0].ParameterType == typeof(string));

您现在可以获取索引器的值并设置其 Value 属性:

object collection = indexProperty.GetValue(userProfile, new object[] { "PictureUrl" });

PropertyInfo valueProperty = collection.GetType().GetProperty("Value");
valueProperty.SetValue(collection, userPictureUrl, null);

如果您有多个匹配的索引属性,您可以通过以下方式找到它:

PropertyInfo indexProperty = (from p in t.GetProperties()
let indexParams = p.GetIndexParameters()
where indexParams.Length == 1 && indexParams[0].ParameterType == typeof(string)
select p).Single();

关于c# - 使用反射设置索引属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12805792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com