MSBuild:描述应该调用哪个构造函数

4

我正在使用MSBuild任务,需要实例化一个类。该类最初只有一个无参数构造函数,因此所有MSBuild任务所需的仅是类型名称来实例化该类。现在我们有一个用例,需要运行特定构造函数的任务,但我不知道如何以通用方式处理这个问题。假设我需要实例化不同版本的ClassA

public class ClassA
{
    public ClassA() { }
    public ClassA(int someArgument) { }
    public ClassA(int someArgument, bool someOtherArgument) { }
}

这是原始任务的样子:

<DoSomethingTask Assembly="ContainsClassA.dll" Type="ClassA" />

我的理想任务应该是这样的,我可以调用任何带有基本类型参数的构造函数:

<DoSomethingTask Assembly="ContainsClassA.dll" Type="ClassA">
    <ConstructorArgs>
        <Arg type="int">1</Arg>
        <Arg type="bool">True</Arg>
    </ConstructorArgs>
</DoSomethingTask>

我很困惑如何搜索以获取这种类型的功能。我可以做一些事情,比如创建一个名为ConstructorArgs的字符串属性,并使用任何我想要的格式进行解析,但我希望有更简洁的方法存在。感谢您能提供的任何帮助!
编辑 - 以防有人想知道,我正在尝试修改的实际任务是基于已实例化的Entity Framework DbContext创建预生成视图。我们有自己的DbContext子类,其中包含各种构造函数,并且我们希望在视图生成过程中调用特定的构造函数。
1个回答

0
你可以尝试使用反射并获取你的DBContext子类的构造函数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace TaskClass
{
    // Class to be created
    public class MyDbContext
    {
        public int ConstructorArg1 { get; set; }
        public string ConstructorArg2 { get; set; }

        public MyDbContext() { }

        public MyDbContext(int constructorArg1)
        {
            ConstructorArg1 = constructorArg1;
        }

        public MyDbContext(int constructorArg1, string constructorArg2)
        {
            ConstructorArg1 = constructorArg1;
            ConstructorArg2 = constructorArg2;
        }       
    }

    // MSBuild custom task
    public class DoSomethingTask : Task
    {
        public override bool Execute()
        {
            var taskParameters = new TaskParametersInfo();
            taskParameters.ExtractTaskParametersInfo(this);

            var type = typeof(MyDbContext);
            ConstructorInfo ctor = type.GetConstructor(taskParameters.Types.ToArray());

            if (ctor == null)
            {
                // If the constructor is not found, throw an error
                Log.LogError("There are no constructors defined with these parameters.");
                return false;
            }

            // Create your instance
            var myDbContext = (MyDbContext)ctor.Invoke(taskParameters.Values.ToArray());

            return true;
        }

        public int ConstructorArg1
        {
            get;
            set;
        }

        public string ConstructorArg2
        {
            get; 
            set;
        }

        public string ConstructorArg3
        { 
            get; 
            set; 
        }

        // Class to handle the task's parameters
        internal class TaskParametersInfo
        {
            public List<Type> Types { get; set; }
            public List<object> Values { get; set; }

            public TaskParametersInfo()
            {
                Types = new List<Type>();
                Values = new List<object>();
            }

            public void ExtractTaskParametersInfo(Task task)
            {
                foreach (var property in task.GetType().GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance))
                {
                    var propertyValue = property.GetValue(task, null);
                    if (propertyValue != null)
                    {
                        Types.Add(property.PropertyType);
                        Values.Add(propertyValue);
                    }
                }
            }
        }
    }
}

将任务导入您的 MSBuild 项目:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Project ToolsVersion="4.0" defaultTarget="DoSomething" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TaskClass.DoSomethingTask" 
        AssemblyFile="TaskClass\TaskClass\bin\Debug\TaskClass.dll"/>
  <Target Name="DoSomething">
    <DoSomethingTask ConstructorArg1="123" ConstructorArg2="Are u talking to me?" />
  </Target>
</Project>

希望这可以帮到你。


这与我在任务中实际要做的相似,但我试图解决的问题涉及未知数量和各种类型的参数。因此,我的构造函数可能是:(string,int,int),(string,bool),(string,bool,bool)。我更多地是寻找如何在MSBuild中提供一个带有基本类型描述符的字符串数组。 - Ocelot20

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接