我一直在浏览网页,但是没有找到任何有用的信息。我需要使用ProfileCommon,但是我无法引用任何程序集来使用它。有人可以帮帮我吗?
我一直在浏览网页,但是没有找到任何有用的信息。我需要使用ProfileCommon,但是我无法引用任何程序集来使用它。有人可以帮帮我吗?
这是由框架生成的动态类型。在运行时,类型“ProfileCommon”存在。
如果您可以使用C# 4.0语言功能,则可以将ProfileCommon的行为封装在动态对象中。
我有以下扩展属性
<profile enabled="true" >
<properties>
<add name="FirstName" type="String" />
<add name="LastName" type="String" />
</properties>
</profile>
使用动态对象的代码示例如下:
dynamic profile = new ProfileData();
var name = profile.FirstName + ' ' + profile.LastName;
ProfileData 的实现:
public class ProfileData: DynamicObject
{
private readonly ProfileBase profileBase;
/// <summary>
/// Profile Data for the current user.
/// </summary>
public ProfileData()
{
profileBase = HttpContext.Current.Profile;
}
/// <summary>
/// Profile data for user with name <paramref name="userName"/>
/// </summary>
/// <param name="userName"></param>
public ProfileData(string userName)
{
profileBase = ProfileBase.Create(userName);
}
// If you try to get a value of a property
// not defined in the class, this method is called.
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
try
{
result = profileBase.GetPropertyValue(binder.Name);
}
catch(SettingsPropertyNotFoundException)
{
result = null;
return false;
}
return true;
}
// If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(SetMemberBinder binder, object value)
{
try
{
profileBase.SetPropertyValue(binder.Name, value);
return true;
}
catch (SettingsPropertyNotFoundException)
{
return false;
}
}
/// <summary>
/// Persist the profile data.
/// </summary>
public void Save()
{
profileBase.Save();
}
}