// Copyright (c) .NET Foundation. 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.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
///
/// Result of .
///
public struct CompilerCacheResult
{
///
/// Initializes a new instance of with the specified
/// .
///
/// Path of the view file relative to the application base.
/// The .
public CompilerCacheResult(string relativePath, CompilationResult compilationResult)
: this(relativePath, compilationResult, new IChangeToken[0])
{
}
///
/// Initializes a new instance of with the specified
/// .
///
/// Path of the view file relative to the application base.
/// The .
/// One or more instances that indicate when
/// this result has expired.
public CompilerCacheResult(string relativePath, CompilationResult compilationResult, IList expirationTokens)
{
if (expirationTokens == null)
{
throw new ArgumentNullException(nameof(expirationTokens));
}
ExpirationTokens = expirationTokens;
var compiledType = compilationResult.CompiledType;
var newExpression = Expression.New(compiledType);
var pathProperty = compiledType.GetProperty(nameof(IRazorPage.Path));
var propertyBindExpression = Expression.Bind(pathProperty, Expression.Constant(relativePath));
var objectInitializeExpression = Expression.MemberInit(newExpression, propertyBindExpression);
PageFactory = Expression
.Lambda>(objectInitializeExpression)
.Compile();
}
///
/// Initializes a new instance of for a file that could not be
/// found in the file system.
///
/// One or more instances that indicate when
/// this result has expired.
public CompilerCacheResult(IList expirationTokens)
{
if (expirationTokens == null)
{
throw new ArgumentNullException(nameof(expirationTokens));
}
ExpirationTokens = expirationTokens;
PageFactory = null;
}
///
/// instances that indicate when this result has expired.
///
public IList ExpirationTokens { get; }
///
/// Gets a value that determines if the view was successfully found and compiled.
///
public bool Success => PageFactory != null;
///
/// Gets a delegate that creates an instance of the .
///
public Func PageFactory { get; }
}
}