C# Accessing dynamic property on interface

1.3k Views Asked by At

I am playing around with FluentSecurity library for asp.net mvc. One of the interfaces exposed by this library is ISecurityContext as shown below:

public interface ISecurityContext
{
    dynamic Data { get; }
    bool CurrenUserAuthenticated();
    IEnumerable<object> CurrenUserRoles();
}

When I try to access "Data" property (as shown below) it is not available. Although the other two methods seems to be accessible.

public class ExperimentalPolicy : ISecurityPolicy
{
    public PolicyResult Enforce(ISecurityContext context)
    {
        dynamic data = context.Data; // Data property is not accessible.
    }
}

What am I missing ? Thanks.

2

There are 2 best solutions below

0
jbtule On BEST ANSWER

The Data property on ISecurityContext isn't introduced until version 2.0. The default installed with nuget without including prerelease is 1.4. Which does not have the property. Make sure you are using the right version!

6
Hogan On

The following ran as expected, is there anything I'm doing differently than you are?

void Main()
{
  ATest t = new ATest();
  Experiment z = new Experiment();

  z.TestTest(t);
}

public class ATest : ITest
{
  public dynamic Data {get; set;}

  public ATest()
  {
     Data = new { Test = "This is a string" };
  }
}

// Define other methods and classes here
public interface ITest
{
  dynamic Data { get; }
}

public class Experiment
{
    public int TestTest(ITest context)
    {
       dynamic data = context.Data; 

       Console.WriteLine(data.Test);

       return 0;
    }
}