Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Razor compilation.Emit(..)使用构造函数参数创建typeOf(T)_Razor_.net Core_Roslyn_Razorengine_Roslyn Code Analysis - Fatal编程技术网

Razor compilation.Emit(..)使用构造函数参数创建typeOf(T)

Razor compilation.Emit(..)使用构造函数参数创建typeOf(T),razor,.net-core,roslyn,razorengine,roslyn-code-analysis,Razor,.net Core,Roslyn,Razorengine,Roslyn Code Analysis,我正在使用RazorEngine进行Razor模板解析,并试图利用它的Roslyn代码 我知道我的问题是我在razor视图中使用了一个基类型(ViewBase),而它没有被构造(因为它有一个ctor参数) 然而,在谷歌搜索了很多次之后,我找不到方法告诉Emit()如何创建我的类型的实例 下面是它使用的代码-源代码生成的代码很好,下面包含一个省略的版本 我是否可以提供某种类型的工厂,用于生成这种类型的产品 当我调用emit时,我得到一个错误没有给出与“ViewBase.ViewBase(IComp

我正在使用
RazorEngine
进行Razor模板解析,并试图利用它的Roslyn代码

我知道我的问题是我在razor视图中使用了一个基类型(
ViewBase
),而它没有被构造(因为它有一个ctor参数)

然而,在谷歌搜索了很多次之后,我找不到方法告诉
Emit()
如何创建我的类型的实例

下面是它使用的代码-源代码生成的代码很好,下面包含一个省略的版本

我是否可以提供某种类型的工厂,用于生成这种类型的产品

当我调用emit时,我得到一个错误
没有给出与“ViewBase.ViewBase(IComponentContext)”的必需形式参数“componentContext”相对应的参数
-那么我如何告诉emit()如何创建我的类型
ViewBase的实例

创建一个空的CTOR很好,但这不是我所需要的

public override Tuple<Type, CompilationData> CompileType(TypeContext context)
        {
            var sourceCode = GetCodeCompileUnit(context);
            var assemblyName = GetAssemblyName(context);

            (new PermissionSet(PermissionState.Unrestricted)).Assert();
            var tempDir = GetTemporaryDirectory();

            var sourceCodeFile = Path.Combine(tempDir, String.Format("{0}.{1}", assemblyName, SourceFileExtension));
            File.WriteAllText(sourceCodeFile, sourceCode);

            var references = GetAllReferences(context);

            var compilation =
                GetEmptyCompilation(assemblyName)
                    .AddSyntaxTrees(
                        GetSyntaxTree(sourceCode, sourceCodeFile))
                    .AddReferences(GetMetadataReferences(references));

            compilation =
                compilation
                    .WithOptions(
                        CreateOptions(context)
                            .WithOutputKind(OutputKind.DynamicallyLinkedLibrary)
                            .WithPlatform(Platform.AnyCpu)
                            .WithSourceReferenceResolver(new RazorEngineSourceReferenceResolver(sourceCodeFile)));

            var assemblyFile = Path.Combine(tempDir, String.Format("{0}.dll", assemblyName));

            var assemblyPdbFile = Path.Combine(tempDir, String.Format("{0}.pdb", assemblyName));
            var compilationData = new CompilationData(sourceCode, tempDir);

            using (var assemblyStream = File.Open(assemblyFile, FileMode.Create, FileAccess.ReadWrite))
            using (var pdbStream = File.Open(assemblyPdbFile, FileMode.Create, FileAccess.ReadWrite))
            {
                var opts = new EmitOptions()
                    .WithPdbFilePath(assemblyPdbFile);
                var pdbStreamHelper = pdbStream;

                if (IsMono())
                {
                    opts = opts.WithDebugInformationFormat(DebugInformationFormat.PortablePdb);
                }

                var result = compilation.Emit(assemblyStream, pdbStreamHelper, options: opts);

            }
        }
public覆盖元组CompileType(类型上下文)
{
var sourceCode=GetCodeCompileUnit(上下文);
var assemblyName=GetAssemblyName(上下文);
(新PermissionSet(PermissionState.Unrestricted)).Assert();
var tempDir=GetTemporaryDirectory();
var sourceCodeFile=Path.Combine(tempDir,String.Format(“{0}.{1}”,assemblyName,SourceFileExtension));
WriteAllText(sourceCodeFile,sourceCode);
var references=GetAllReferences(上下文);
变量编译=
GetEmptyCompilation(assemblyName)
.AddSyntaxTrees(
GetSyntaxTree(源代码,源代码文件))
.AddReferences(GetMetadataReferences(references));
汇编=
汇编
.有选择权(
CreateOptions(上下文)
.WithOutputKind(OutputKind.DynamicallyLinkedLibrary)
.WithPlatform(Platform.AnyCpu)
.WithSourceReferenceResolver(新RazorEngineSourceReferenceResolver(sourceCodeFile));
var assemblyFile=Path.Combine(tempDir,String.Format(“{0}.dll”,assemblyName));
var assemblyPdbFile=Path.Combine(tempDir,String.Format(“{0}.pdb”,assemblyName));
var compliationdata=新的compliationdata(源代码,tempDir);
使用(var assemblyStream=File.Open(assemblyFile,FileMode.Create,FileAccess.ReadWrite))
使用(var pdbStream=File.Open(assemblyPdbFile,FileMode.Create,FileAccess.ReadWrite))
{
var opts=new EmitOptions()
.WithPdbFilePath(assemblyPdbFile);
var pdbStream helper=pdbStream;
if(IsMono())
{
opts=opts.WithDebugInformationFormat(DebugInformationFormat.PortablePdb);
}
var result=compilation.Emit(assemblyStream,pdbStreamHelper,选项:opts);
}
}
我生成的视图代码

namespace CompiledRazorTemplates.Dynamic
{

#line default
#line hidden
    ;
    using System;
    //my load of other using statements...

    public class RazorEngine_99d043dd3e3d4c3ca787d42dd7a0bb6b : ViewBase<MyModelType>
    {
        #line hidden
        public RazorEngine_99d043dd3e3d4c3ca787d42dd7a0bb6b()
        {
        }

        #pragma warning disable 1998
        public override async Task Execute()
        {
..... OMITTED
        }
    }
}


public ViewBase(IComponentContext componentContext)
{
    Contract.Requires(componentContext != null);

    _componentContext = componentContext;
}
namespace CompiledRazorTemplates.Dynamic
{
#行默认值
#隐藏线
;
使用制度;
//我的其他使用语句的负载。。。
公共类RazorEngine_99d043dd3e3d4c3ca787d42dd7a0bb6b:视图库
{
#隐藏线
公共RazorEngine_99D043DD3E3D4D4C3CA787D42DD7A0BB6B()
{
}
#布拉格警告禁用1998
公共重写异步任务执行()
{
……省略
}
}
}
公共视图库(IComponentContext组件上下文)
{
Contract.Requires(componentContext!=null);
_componentContext=componentContext;
}

你问的是Razor编译器,而不是Roslyn编译

我认为没有任何方法可以做到这一点


但是,您可以使用Roslyn
CSharpSyntaxRewriter
SyntaxTree
中查找现有构造函数,并将其重写为具有并传递参数。

您所问的是Razor编译器,而不是Roslyn编译

我认为没有任何方法可以做到这一点


但是,您可以使用Roslyn
CSharpSyntaxRewriter
SyntaxTree
中查找现有构造函数,并将其重写为具有并传递参数。

您的问题不清楚。您是否在询问如何使Razor编译器生成一个使用参数调用基类ctor的ctor?抱歉-是的,当我调用
emit
时,我得到错误
没有与所需的形式参数“ViewBase.ViewBase(IComponentContext)”相对应的参数“
-那么我该如何告诉
emit()
如何创建我类型的实例
ViewBase
创建一个空的CTOR很好-但这不是我需要的。你的问题不清楚。您是否在询问如何使Razor编译器生成一个使用参数调用基类ctor的ctor?抱歉-是的,当我调用
emit
时,我得到错误
没有与所需的形式参数“ViewBase.ViewBase(IComponentContext)”相对应的参数“
-那么我该如何告诉
emit()
如何创建我类型的实例
ViewBase
创建一个空的CTOR很好-但这不是我需要的。它当前有一个参数-我只需要让Roslyn pass说一个参数-如果我提供我的
ViewBase
…更新,会有更多帮助-你的答案仍然适用吗?”?如果有,你有什么例子吗?Thanks@Stuart.Sklinar:这正是我要说的。您应该在语法树中编辑ctor。非常好。我会进一步调查的。ThanksIt目前有一个参数-我只需要让Roslyn pass说param-如果我提供我的
viewBase
…更新,它会帮助更多-您的答案仍然适用吗?如果有,你有什么例子吗?Thanks@Stuart.Sklinar:那正是