在做了一些测试后,我将 Utilities.Serialize... 代码放入工作的 FullName 中,这很有效。然后我将 FullName 重命名为 FullNameXX,但它失败了。经过几次测试后,我得出的结论是,要保存在 Profile 中的所有属性都需要是字符串(或者我假设是二进制,但我没有使用 SQL 中的 PropertyValuesBinary 数据字段。)所以当我有一个复杂的字段时,就像我的 List 一样,我有一个用于获取和设置 List 的程序员的属性以及存储的同一属性的字符串版本。所以我现在有一个 SectionSettings 和一个 SectionSettingsValue 属性。需要进行的另一个调整是使其仅成为 DataContract 而不是可序列化的,否则您将获得额外的值,例如 k__backingField 恕我直言看起来很糟糕。
以下应该是完整的代码...
这是我的数据合同:
[DataContract]
public class UserProfileContract : ProfileBase
{
#region Constructors
public UserProfileContract()
{
} // UserProfileContract - Constructor
public UserProfileContract(List<SettingSection> SectionSettings)
{
this.SectionSettings = SectionSettings;
} // UserProfileContract - Constructor
#endregion Constructors
public static UserProfileContract CurrentUser
{
get { return (UserProfileContract)(ProfileBase.Create(Membership.GetUser().UserName)); }
}
public string FullNameValue { get; set; }
public string SectionSettingsValue { get; set; }
[DataMember(Name = "FullName")]
public string FullName
{
get { return ((string)(base["FullNameValue"])); }
set {
base["FullNameValue"] = value;
Save();
}
} // FullName - Property
[DataMember(Name = "SectionSettings")]
public List<SettingSection> SectionSettings
{
get { return Utilities.Deserialize<List<SettingSection>>(base["SectionSettingsValue"].ToString()); }
set
{
base["SectionSettingsValue"] = Utilities.Serialize<List<SettingSection>>(value);
Save();
}
} // SectionSettings - Property
} // UserProfileContract - Class
[DataContract]
public class SettingSection
{
public SettingSection()
{
this.UserSettings = new List<UserSettingPair>();
} // SettingSection - Constructor
public SettingSection(List<UserSettingPair> UserSettings)
{
this.UserSettings = UserSettings;
} // SettingSection - Constructor
[DataMember]
public string SectionName { get; set; }
[DataMember]
public List<UserSettingPair> UserSettings { get; set; }
} // SettingSection - Class
[DataContract]
public class UserSettingPair
{
[DataMember]
public string Key { get; set; }
[DataMember]
public string Value { get; set; }
} // UserSettingPair - Class
这是我的静态实用程序类:
public static T Deserialize<T>(string json)
{
var obj = Activator.CreateInstance<T>();
if (string.IsNullOrWhiteSpace(json))
return obj;
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
var serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
return obj;
} // using the memory stream
} // Deserialize - Method
public static string Serialize<T>(object input)
{
string Result = "";
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
ser.WriteObject(ms, input);
Result = Encoding.Default.GetString(ms.ToArray());
}
return Result;
} // Serialize - Method
以下是如何在多个部分中保存数据以及 FullName 属性的示例:
// First Section
SettingSection s = new SettingSection();
s.SectionName = "ContractSearch";
UserSettingPair usp = new UserSettingPair();
usp.Key = "Default Control";
usp.Value = "txtFirstTextBox";
s.UserSettings.Add(usp);
usp = new UserSettingPair();
usp.Key = "Field1Choice";
usp.Value = "SchoolName";
s.UserSettings.Add(usp);
List<SettingSection> ss = new List<SettingSection>();
ss.Add(s);
// Second Section
s = new SettingSection();
s.SectionName = "Workflow Settings";
usp = new UserSettingPair();
usp.Key = "Primart Thing";
usp.Value = "Blabla bla";
s.UserSettings.Add(usp);
usp = new UserSettingPair();
usp.Key = "Allowable Tries";
usp.Value = "3";
s.UserSettings.Add(usp);
usp = new UserSettingPair();
usp.Key = "Extra Value";
usp.Value = "Gigity";
s.UserSettings.Add(usp);
ss.Add(s);
UserProfileContract.CurrentUser.FullName = "Grigsby";
UserProfileContract.CurrentUser.SectionSettings = ss;
这是我创建的一个扩展方法,可以更轻松地提取 SectionSetting 值:
public static T GetSectionValue<T>(this UserProfileContract up, string Section, string Property)
{
string value = (from ss in up.SectionSettings
from us in ss.UserSettings
where ss.SectionName == Section
&& us.Key == Property
select us.Value).FirstOrDefault();
try
{
return (T)Convert.ChangeType(value, typeof(T));
}
catch (InvalidCastException)
{
return default(T);
}
} // GetSectionValue - Extension Method
最后,上面的扩展方法的一个例子:
string k = x.GetSectionValue<string>("Workflow Settings", "Primary Thing");
string g = x.GetSectionValue<string>("Workflow Settings", "Extra Value");
int three = x.GetSectionValue<int>("Workflow Settings", "Allowable Tries");
这是我输入的值的字符串版本:
[{"SectionName":"ContractSearch","UserSettings":[{"Key":"Default Control","Value":"txtFirstTextBox"},{"Key":"Field1Choice","Value":"SchoolName"}]},{"SectionName":"Workflow Settings","UserSettings":[{"Key":"Primart Thing","Value":"Blabla bla"},{"Key":"Allowable Tries","Value":"3"},{"Key":"Extra Value","Value":"Gigity"}]}]Grigsby
在上面的字符串 k = ... 示例中,请注意它返回 null 因为数据是“Primart Thing”而不是“Primary Thing”。
希望这可以帮助那里的人。