我使用SqlTableProfileProvider作为我的个人资料提供者,并呼吁“的ProfileCommon”从System.Web.Profile.ProfileBase继承的自定义类。我的ProfileCommon类属性表示我的概要文件表中的列,每个属性都获得[CustomProviderData(“columnName; dbType”)]的属性。我想添加一个新的属性到特定的列,然后从SqlTableProfileProvider类中获取这些信息。SqlTableProfileProvider,自定义ProfileBase类,添加额外的属性特性
我和团队正在寻找将外键(表)与我们的个人资料表相关联。现在,我们的配置文件表基本上存储了键值对,名字,姓氏,年龄等;不过,我们正在计划存储书签,链接到最喜欢的文章,以及不会的内容,这些内容将显示在我们的仪表板页面的列表中。我们喜欢使用SqlTableProfileProvider和我创建的ProfileCommon对象。我们所有的asp.net页面都从一个BasePage继承而来,一个名为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) { ... }
例:
public class ProfileCommon : System.Web.Profile.ProfileBase
{
public static ProfileCommon GetProfile() { .... }
[CustomProviderData("FirstName;varchar")]
public virtual string FirstName
{
get
{
return ((string)(this.GetPropertyValue("FirstName")));
}
set
{
this.SetPropertyValue("FirstName", value);
}
}
[CustomProviderData("LastName;varchar")]
public virtual string LastName
{
get
{
return ((string)(this.GetPropertyValue("LastName")));
}
set
{
this.SetPropertyValue("LastName", value);
}
}
[CustomProviderData("OtherColumn;int")]
[TableNameData("OtherTable")]
public virtual int OtherColumn
{
get ...
set ...
}
}
// My new attribute
[AttributeUsage(AttributeTargets.Property)]
public class TableNameData : Attribute
{
private string _tableName;
public TableNameData(string tableName)
{
_tableName = tableName;
}
public string TableName
{
get
{
return _tableName;
}
}
}
// Not my implementation, but looking to enhance it.
public class SqlTableProfileProvider : ProfileProvider
{
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyValueCollection svc, string username, SqlConnection conn)
{
...
foreach (SettingsProperty prop in properties)
{
...
// in here, gets CustomProviderData
string persistenceData = prop.Attributes["CustomProviderData"] as string.
// how do i get to mine?
}
}
}
的SqlTableProfileProvider由郝刳嗯实现。它从ProfileProvider继承。
GetPropertyValues方法之一返回SettingsPropertyValueCollection。有一个名为GetProfileDataFromTable的私有方法。在那里,我希望访问我创建的自定义属性。
问:我如何进入我的属性,我已经在我的财产指定?
UPDATE: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]);
}
}
}
}
}
}
}
}