This file contains hidden or 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
using System; | |
using Microsoft.Build.Construction; | |
public static class ProjectRootElementExtensions | |
{ | |
public static void AddUsingTask<TTask>(this ProjectRootElement projectRootElement) | |
{ | |
Type taskType = typeof(TTask); | |
projectRootElement | |
.AddUsingTask(taskType.Name, taskType.Assembly.Location, null); |
This file contains hidden or 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
var projectRootElement = ProjectRootElement.Create(); | |
projectRootElement | |
.AddTarget("Build") | |
.AddTask<MyTask>() | |
.SetParameter("Foo", "hi"); | |
var highImportanceStringLogger = new HighImportanceStringLogger(); | |
var success = new ProjectInstance(projectRootElement).Build(new[] { highImportanceStringLogger }); |
This file contains hidden or 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
public abstract class LoggingTask : Task | |
{ | |
protected void LogInputs() | |
{ | |
Log.LogMessage(MessageImportance.High, "Inputs:"); | |
foreach (var propertyInfo in GetInputProperties()) | |
{ | |
LogProperty(propertyInfo); | |
} |
This file contains hidden or 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
public class TaskWithRequiredInput : LoggingTask | |
{ | |
[Required] | |
public string Foo { get; set; } | |
public override bool Execute() | |
{ | |
LogInputs(); | |
return true; | |
} |
This file contains hidden or 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
public static class PropertyInfoExtensions | |
{ | |
public static bool HasAttribute<TAttribute>(this PropertyInfo propertyInfo) | |
where TAttribute : Attribute | |
{ | |
return propertyInfo.GetCustomAttribute<TAttribute>() != null; | |
} | |
} |
This file contains hidden or 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
[TestMethod] | |
public void RequiredInputGetsLogged() | |
{ | |
var buildTarget = CreateBuildTarget(); | |
buildTarget.AddTask<TaskWithRequiredInput>().SetParameter("Foo", "hi"); | |
const string expected = @"Inputs: | |
Foo = hi | |
"; |
This file contains hidden or 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
private void LogProperty(PropertyInfo propertyInfo) | |
{ | |
string name = propertyInfo.Name; | |
var value = propertyInfo.GetValue(this); | |
if (value is ITaskItem[]) | |
{ | |
LogTaskItems(value as ITaskItem[], name); | |
} | |
else | |
{ |
This file contains hidden or 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
class AssertNoBindingErrorsTraceListener : TraceListener | |
{ | |
readonly StringBuilder messageBuilder = new StringBuilder(); | |
public AssertNoBindingErrorsTraceListener(SourceLevels level) | |
{ | |
PresentationTraceSources.DataBindingSource.Listeners.Add(this); | |
PresentationTraceSources.DataBindingSource.Switch.Level = level; | |
} |
This file contains hidden or 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
new Dictionary<int, string> | |
{ | |
{ 3, "Fizz"}, | |
{ 5, "Buzz"}, | |
{15, "FizzBuzz"}, | |
} | |
.Where(item => i % item.Key == 0) | |
.Select(item => item.Value) | |
.LastOrDefault() ?? i.ToString(); |
This file contains hidden or 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
interface I | |
{ | |
string Get(int i); | |
} | |
private class Fizz : I | |
{ | |
public string Get(int i) | |
{ | |
return "Fizz"; |