The action needs to be created before the QueryPropertyMessage
has been created, or even defined, for you to be able to pass it into its constructor. There is therefore nothing to reference at that point in time. You need to create, or at least define a variable for, the object in question first.
QueryPropertyMessage message = new QueryPropertyMessage();
message.Callback = () =>
{
var propertyName = message.PropertyName;
};
(This will of course require having a parameterless constructor and a setter for the callback.)
If you don't want to modify the class, then you can only define, and not initialize, the variable:
QueryPropertyMessage message = null;
message = new QueryPropertyMessage(() =>
{
var propertyName = message.PropertyName;
});
That, or you need to pass the object itself as a parameter to the action:
public class QueryPropertyMessage
{
public Action<QueryPropertyMessage> Callback { get; private set; }
public string PropertyName { get; set; }
public object PropertyValue { get; set; }
public QueryPropertyMessage(Action<QueryPropertyMessage> callback)
{
this.Callback = callback;
}
}
new QueryPropertyMessage(props =>
{
var propertyName = props.PropertyName;
});