【问题标题】:Access another user's custom profile property that has a custom type (class)访问具有自定义类型(类)的另一个用户的自定义配置文件属性
【发布时间】:2014-01-28 20:50:18
【问题描述】:

我正在使用 ASP.net 和 C#,试图从 web.config 文件访问另一个用户的 Profile 属性。

我要访问的属性具有我创建的类的自定义类型ClassesList

public class ClassesList
{
    public struct Class_
    {
        public string name;
        public string teacherName;
        public string startDate, endDate;
        public int numOfEnrolledStudents;
        public int maxNumOfStudents;
        public string[] studentsNames;
        public string accessKey;
        public bool IsActive;
     }

     public Class_[] Classes;

     public ClassesList()
     {
        this.Classes = new Class_[200];

        for (int i = 0; i < 200; i++)
        {
            Classes[i].IsActive = false;
            Classes[i].studentsNames = new string[500];
        }
     }
}

这是我在 web.config 文件中的个人资料:

<profile>
  <properties>
    <add name="ClassList" type="ClassesList" allowAnonymous="false" />
  </properties>
</profile>

我正在使用此语句获取另一个用户的个人资料(在 for 循环中)

ProfileBase currentTeacherProfile = ProfileBase.Create(teacherList[i], true);

所以现在我有了个人资料,我只需要访问我想要的个人资料属性,那就是accessKey

我正在尝试使用此代码来获取属性,但我得到了未找到的异常

currentTeacherProfile.GetPropertyValue("ClassList.Classes[0].accessKey")

注意:使用

访问当前用户的配置文件属性
Profile.ClassList.Classes[0].accessKey

有效,但在其他用户的情况下无效。

谢谢。

【问题讨论】:

    标签: c# asp.net profile


    【解决方案1】:
    ProfileCommon otherProfile = Profile.getProfile("name");
    

    【讨论】:

      【解决方案2】:

      需要先获取属性值(ClassesList)

      ClassesList classList = currentTeacherProfile.GetPropertyValue("ClassList") as ClassesList;
      

      然后,您可以访问该对象的属性

      classList.Classes[0].accessKey
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-12
        • 2012-02-12
        • 1970-01-01
        • 2018-10-05
        • 1970-01-01
        • 2018-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多