C# SqlTableProfileProvider,自定义Profile基类,向属性添加额外属性

C# SqlTableProfileProvider,自定义Profile基类,向属性添加额外属性,c#,asp.net,C#,Asp.net,我使用SqlTableProfileProvider作为我的配置文件提供程序,并从System.Web.profile.ProfileBase继承一个名为“ProfileCommon”的自定义类。My ProfileCommon类属性表示我的配置文件表中的列,每个属性的属性为[CustomProviderData(“columnName;dbType”)]。我正在尝试向特定列添加一个新属性,然后将从SqlTableProfileProvider类中提取该信息 我和团队希望将外键(表)与配置文件表

我使用SqlTableProfileProvider作为我的配置文件提供程序,并从System.Web.profile.ProfileBase继承一个名为“ProfileCommon”的自定义类。My ProfileCommon类属性表示我的配置文件表中的列,每个属性的属性为[CustomProviderData(“columnName;dbType”)]。我正在尝试向特定列添加一个新属性,然后将从SqlTableProfileProvider类中提取该信息

我和团队希望将外键(表)与配置文件表相关联。现在,我们的Profile表基本上存储键值对、FirstName、LastName、Age等;不过,我们计划存储书签、最喜爱文章的链接以及其他内容,这些内容将显示在仪表板页面的列表中。我们喜欢使用SqlTableProfileProvider和我创建的ProfileCommon对象。我们所有的asp.net页面都继承自一个BasePage,一个名为Profile的属性获取Profile公共对象

如果能够做到以下几点就好了:

Profile.Bookmarks.Count; // to know if there are bookmarks

// to also just be able to foreach through them
foreach (Bookmark bk in Profile.Bookmarks) { ... }
例:

SqlTableProfileProvider由Hao Kung实现。它继承自ProfileProvider

其中一个方法GetPropertyValues返回SettingsPropertyValueCollection。有一个名为GetProfileDataFromTable的私有方法。在这里,我希望访问我创建的自定义属性

问题:如何访问我在属性上指定的属性

更新:07162011:1517,问题提出后7天

我确实找到了一个方法。以下是我是如何做到这一点的:

// In the default constructor add the following
public ProfileCommon()
{
    //  Get all properties for this class 'ProfileCommon'
    PropertyInfo[] propertyInfos = typeof(ProfileCommon).GetProperties();

    //  The ProfileBase, base class, has a property called Properties and 
    //  one can get to all attributes on that property but there are only
    //  a few attributes that ProfileBase looks for. If the developer wishes
    //  to use custom attributes on a property, it wont appear in the 
    //  ProfileCommon.Properties.Attributes list.
    //  
    //  So, what are we going to do, well, we are going to come up with a hack and solution to this problem
    //
    foreach (SettingsProperty settingsProperty in ProfileCommon.Properties)
    {
        foreach (PropertyInfo propertyInfo in propertyInfos)
        {
            if (settingsProperty.Name == propertyInfo.Name)
            {
                //  get all attributes from the associated property, but we are getting it from the propertyInfo variable
                //  which was retrieved through reflection and will list ALL attributes.
                    object[] attributes = propertyInfo.GetCustomAttributes(false);

                for (int i = 0; i < attributes.Count(); i++)
                {
                    Type type = attributes[i].GetType();

                    PropertyInfo[] attributeClassProperities = type.GetProperties();

                    foreach (PropertyInfo attributeClassProperty in attributeClassProperities)
                    {
                        //  not intested in the TypeId property for the object
                        if (!attributeClassProperty.Name.Equals("TypeId"))
                        {
                            //  if the settingsProperty.Attributes does not contain our key value pair, then add it.
                            if (settingsProperty.Attributes[attributeClassProperty.Name] == null)
                            {
                                    settingsProperty.Attributes.Add(attributeClassProperty.Name, attributes[i]);
                            }
                        }
                    }
                }
            }
        }
    }
}
//在默认构造函数中添加以下内容
公共档案
{
//获取此类“ProfileCommon”的所有属性
PropertyInfo[]propertyInfos=typeof(ProfileCommon).GetProperties();
//ProfileBase,基类,有一个名为Properties和Properties的属性
//可以访问该属性上的所有属性,但只有
//ProfileBase查找的几个属性。如果开发人员愿意
//若要在属性上使用自定义属性,它不会显示在
//ProfileCommon.Properties.Attributes列表。
//  
//那么,我们将要做什么呢?好吧,我们将想出一个解决这个问题的方法
//
foreach(设置属性设置ProfileCommon.Properties中的属性)
{
foreach(PropertyInfo PropertyInfo in propertyInfos中的PropertyInfo)
{
if(settingsProperty.Name==propertyInfo.Name)
{
//从关联的属性中获取所有属性,但我们从propertyInfo变量中获取
//它是通过反射检索的,将列出所有属性。
object[]attributes=propertyInfo.GetCustomAttributes(false);
对于(int i=0;i
您只需要像示例中一样注意实现ProfileCommon,并确保属性正确

对于专用的profile属性书签,您还可以编写自己的profile类,就像在示例中一样。在这里,您必须实现一个Count属性,该属性将查询数据库中该用户的书签数量

在数据库的Profile表中,您将有一个Bookmarks列,该列具有合适的FK类型,例如Guid、Int或BigInt,它只不过是另一个表的外键,您可以命名为[dbo]。[Bookmarks],它实际上包含每个用户的书签。此Bookmarks表应具有类似UserId的列,该列将是ASP.NET UserId的FK

// In the default constructor add the following
public ProfileCommon()
{
    //  Get all properties for this class 'ProfileCommon'
    PropertyInfo[] propertyInfos = typeof(ProfileCommon).GetProperties();

    //  The ProfileBase, base class, has a property called Properties and 
    //  one can get to all attributes on that property but there are only
    //  a few attributes that ProfileBase looks for. If the developer wishes
    //  to use custom attributes on a property, it wont appear in the 
    //  ProfileCommon.Properties.Attributes list.
    //  
    //  So, what are we going to do, well, we are going to come up with a hack and solution to this problem
    //
    foreach (SettingsProperty settingsProperty in ProfileCommon.Properties)
    {
        foreach (PropertyInfo propertyInfo in propertyInfos)
        {
            if (settingsProperty.Name == propertyInfo.Name)
            {
                //  get all attributes from the associated property, but we are getting it from the propertyInfo variable
                //  which was retrieved through reflection and will list ALL attributes.
                    object[] attributes = propertyInfo.GetCustomAttributes(false);

                for (int i = 0; i < attributes.Count(); i++)
                {
                    Type type = attributes[i].GetType();

                    PropertyInfo[] attributeClassProperities = type.GetProperties();

                    foreach (PropertyInfo attributeClassProperty in attributeClassProperities)
                    {
                        //  not intested in the TypeId property for the object
                        if (!attributeClassProperty.Name.Equals("TypeId"))
                        {
                            //  if the settingsProperty.Attributes does not contain our key value pair, then add it.
                            if (settingsProperty.Attributes[attributeClassProperty.Name] == null)
                            {
                                    settingsProperty.Attributes.Add(attributeClassProperty.Name, attributes[i]);
                            }
                        }
                    }
                }
            }
        }
    }
}