使用反射设置索引属性的值 [英] Using Reflection to set the value of an indexed property

查看:71
本文介绍了使用反射设置索引属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用反射复制以下 c# 代码:

I am try to replicate the following c# code using reflection:

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"索引属性的那一行之外,我可以让一切正常工作.使用反编译器时,索引属性如下所示:

Using reflection I can get everything to work except for the line where I'm trying to set the "PictureUrl" indexed property on the UserProfile object. That indexed property looks like this when using a decompiler:

public UserProfileValueCollection this[string strPropName]

这是我使用反射来实现与上述相同的代码,注意 TODO 注释,我需要在其中设置 PictureUrl 索引属性的值:

And here is my code using reflection to achieve the same thing as the above, notice the TODO comment where I need to set the value of the PictureUrl indexed property:

   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 上只有一个索引器:

Assuming you only have one indexer on UserProfile:

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

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

You can now get the value for the indexer and set its Value property:

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

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

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

If you have more than one matching index property you can find it with:

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

这篇关于使用反射设置索引属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆