This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for activating view properties
Fixes #700
- Loading branch information
Showing
26 changed files
with
757 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.AspNet.Mvc | ||
{ | ||
internal class PropertyActivator<TContext> | ||
{ | ||
private readonly Func<TContext, object> _valueAccessor; | ||
private readonly Action<object, object> _fastPropertySetter; | ||
|
||
public PropertyActivator(PropertyInfo propertyInfo, | ||
Func<TContext, object> valueAccessor) | ||
{ | ||
PropertyInfo = propertyInfo; | ||
_valueAccessor = valueAccessor; | ||
_fastPropertySetter = PropertyHelper.MakeFastPropertySetter(propertyInfo); | ||
} | ||
|
||
public PropertyInfo PropertyInfo { get; private set; } | ||
|
||
public object Activate(object view, TContext context) | ||
{ | ||
var value = _valueAccessor(context); | ||
_fastPropertySetter(view, value); | ||
return value; | ||
} | ||
|
||
/// <summary> | ||
/// Returns a list of properties on a type that are decorated with | ||
/// the specified activateAttributeType and have setters. | ||
/// </summary> | ||
public static PropertyActivator<TContext>[] GetPropertiesToActivate( | ||
Type type, | ||
Type activateAttributeType, | ||
Func<PropertyInfo, PropertyActivator<TContext>> createActivateInfo) | ||
{ | ||
return type.GetRuntimeProperties() | ||
.Where(property => | ||
property.IsDefined(activateAttributeType) && | ||
property.GetIndexParameters().Length == 0 && | ||
property.SetMethod != null && | ||
!property.SetMethod.IsStatic) | ||
.Select(createActivateInfo) | ||
.ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNet.Mvc.Razor.Host; | ||
|
||
namespace Microsoft.AspNet.Mvc.Razor | ||
{ | ||
/// <summary> | ||
/// Represents information about an injected property. | ||
/// </summary> | ||
public class InjectDescriptor | ||
{ | ||
public InjectDescriptor(string typeName, string memberName) | ||
{ | ||
if (string.IsNullOrEmpty(typeName)) | ||
{ | ||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpy, "typeName"); | ||
} | ||
|
||
if (string.IsNullOrEmpty(memberName)) | ||
{ | ||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpy, "memberName"); | ||
} | ||
|
||
TypeName = typeName; | ||
MemberName = memberName; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the type name of the injected property | ||
/// </summary> | ||
public string TypeName { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the name of the injected property. | ||
/// </summary> | ||
public string MemberName { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.