0

これはasp.netページにのみ適用されると思われますが、これによると:

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

web.config次のようにプロパティを定義できます。

<profile>
  <properties>
    <add name="PostalCode" />
  </properties>
</profile>

次に、次のように使用します。

Profile.PostalCode = txtPostalCode.Text;

しかし、これはコントローラー内でコンパイルされません。

public ActionResult Index()
{
  Profile.PostalCode = "codeofpost";
  return View();
}

ProfileタイプProfileBaseで動的ではないため、これがどのように機能するかはわかりませんが、ドキュメントにはそうではないと書かれています。

4

2 に答える 2

0

不可能だと言われたので、動的を使用して自分でこれを行うことにしました。結局、それは単なる構文糖衣だと思います。

これから降りると、Profile.PostalCode = "codeofpost";

/// <summary>
/// Provides a dynamic Profile.
/// </summary>
public abstract class ControllerBase : Controller
{
    private readonly Lazy<DynamicProfile> _dProfile;

    protected ControllerBase()
    {
        _dProfile = new Lazy<DynamicProfile>(() => new DynamicProfile(base.Profile));
    }

    private sealed class DynamicProfile : DynamicObject
    {
        private readonly ProfileBase _profile;

        public DynamicProfile(ProfileBase profile)
        {
            _profile = profile;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = _profile.GetPropertyValue(binder.Name);
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            _profile.SetPropertyValue(binder.Name, value);
            _profile.Save();
            return true;
        }
    }

    /// <summary>
    /// New dynamic profile, can now access the properties as though they are on the Profile,
    /// e.g. Profile.PostCode
    /// </summary>
    protected new dynamic Profile
    {
        get { return _dProfile.Value; }
    }

    /// <summary>
    /// Provides access to the original profile object.
    /// </summary>
    protected ProfileBase ProfileBase
    {
        get { return base.Profile; }
    }
}
于 2013-09-05T14:03:13.080 に答える