Open sandboxFocusImprove this doc

PS0166: Unknown Field or Property in a Configuration File

Severity Error
Reported by PostSharp
Message The type '<Type>' does not have a public field or property named '<Name>'.

Cause

In some sections of a PostSharp configuration file, such as the Multicast section and the logging profiles, each XML element describes a CLR object, and each XML attribute of the element sets a field or a property of this object. In the logging profiles, a child element can also set a field or a property.

PostSharp reports PS0166 when <Type> has no field or property named <Name>, or when the field or property is not public. Names are case-sensitive. PostSharp skips the attribute and goes on with the others, but the build fails.

In the following example, the name of the Category property is misspelled.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Multicast xmlns:app="clr-namespace:Errors.PS0166_Error;assembly:PS0166_Error">
    <!-- LogAttribute has no property named Categry. -->
    <app:LogAttribute AttributeTargetTypes="Errors.PS0166_Error.OrderService" AttributeTargetMembers="PlaceOrder" Categry="Orders" />
  </Multicast>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0166_Error;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public string Category { get; set; } = "General";

    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"[{this.Category}] Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    // The Multicast element of postsharp.config applies [Log] to this method.
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
error PS0166: postsharp.config(5,115): The type 'Errors.PS0166_Error.LogAttribute' does not have a public field or property named 'Categry'.

Resolution

  • Correct the name of the attribute so that it matches the name of the field or property exactly, with its case.
  • When the field or property is not public, make it public, or remove the attribute.

In the following example, the attribute has the name of the Category property, and the aspect receives its value.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Multicast xmlns:app="clr-namespace:Errors.PS0166_Fixed;assembly:PS0166_Fixed">
    <!-- The attribute has the name of the Category property. -->
    <app:LogAttribute AttributeTargetTypes="Errors.PS0166_Fixed.OrderService" AttributeTargetMembers="PlaceOrder" Category="Orders" />
  </Multicast>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0166_Fixed;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public string Category { get; set; } = "General";

    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"[{this.Category}] Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    // The Multicast element of postsharp.config applies [Log] to this method.
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
[Orders] Entering PlaceOrder.
Order placed for Coffee.

See Also

Other Resources

Including CLR Objects in Configuration
Adding Aspects Using XML