Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read/get a PropertyGroup value from a .csproj file using C# in a .NET Core 2 classlib project?

I want to get the value of the element <Location>SourceFiles/ConnectionStrings.json</Location> that is child of <PropertyGroup /> using C#. This is located at the .csproj file for a .NET Core 2 classlib project. The structure is as follow:

<PropertyGroup>
  <TargetFramework>netcoreapp2.0</TargetFramework>
  <Location>SharedSettingsProvider.SourceFiles/ConnectionStrings.json</Location>
</PropertyGroup>

Which class can I use from .NET Core libraries to achieve this? (not .NET framework)

Update 1: I want to read the value when the application (that this .csproj file builds) runs. Both before and after deployment.

Thanks

like image 702
walter_dl Avatar asked Mar 27 '18 21:03

walter_dl


People also ask

Is it possible to read csproj content at run-time?

Update 1: I want to read the value when the application (that this .csproj file builds) runs. Both before and after deployment. Show activity on this post. As has been discussed in comments, csproj content only controls predefined build tasks and aren't available at run-time.

What is a propertygroup?

A user defined property name, which contains the property value. There may be zero or more Property elements in a PropertyGroup element. Required root element of an MSBuild project file.

What is the use of Property Group in MSBuild?

PropertyGroup element (MSBuild) Contains a set of user-defined Property elements. Every Property element used in an MSBuild project must be a child of a PropertyGroup element.

What is the difference between csproj and MSBuild?

As has been discussed in comments, csproj content only controls predefined build tasks and aren't available at run-time. But msbuild is flexible and other methods could be used to persist some values to be available at run time. Thanks for contributing an answer to Stack Overflow!


1 Answers

As has been discussed in comments, csproj content only controls predefined build tasks and aren't available at run-time.

But msbuild is flexible and other methods could be used to persist some values to be available at run time.

One possible approach is to create a custom assembly attribute:

[System.AttributeUsage(System.AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
sealed class ConfigurationLocationAttribute : System.Attribute
{
    public string ConfigurationLocation { get; }
    public ConfigurationLocationAttribute(string configurationLocation)
    {
        this.ConfigurationLocation = configurationLocation;
    }
}

which can then be used in the auto-generated assembly attributes from inside the csproj file:

<PropertyGroup>
  <ConfigurationLocation>https://my-config.service/customer2.json</ConfigurationLocation>
</PropertyGroup>
<ItemGroup>
  <AssemblyAttribute Include="An.Example.ConfigurationLocationAttribute">
    <_Parameter1>"$(ConfigurationLocation)"</_Parameter1>
  </AssemblyAttribute>
</ItemGroup>

And then used at run time in code:

static void Main(string[] args)
{
    var configurationLocation = Assembly.GetEntryAssembly()
        .GetCustomAttribute<ConfigurationLocationAttribute>()
        .ConfigurationLocation;
    Console.WriteLine($"Should get config from {configurationLocation}");
}
like image 56
Martin Ullrich Avatar answered Oct 13 '22 01:10

Martin Ullrich