.netfastmember

Cannot run FastMember based console app


I'm just playing around with FastMember and have hit a problem.

Each time I start my console app I get the following exception:

Could not load type 'FastMember.ObjectAccessor' from assembly 'FastMember, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

I installed FastMember via NuGet, and here is my code:

using System;

public class Program
{
    private static void Main(string[] args)
    {
        var p = new Person();

        var accessor = ObjectAccessor.Create(p);

        Console.WriteLine(accessor["GetHtml"]);
    }
}

public sealed class Person
{
    public string GetHtml()
    {
        return "";
    }
}

Not sure what I'm doing wrong here. Can I use FastMember like this?


Solution

  • I am not seeing your problem. Would like to mention that the code as you have it does not work.

    I did not get it working with functions, but your code looks like it could be a property, all the same...

    using System;
    using FastMember;
    
    public class Program
    {
       private static void Main(string[] args)
       {
          var p = new Person();
    
          var accessor = ObjectAccessor.Create(p);
    
          Console.WriteLine(accessor["Html"]);
       }
    }
    
    public sealed class Person
    {
       public string Html
       {
          get
          {
             return "<a/>";
          }
       }
    }
    

    EDIT It's instructive to have a look at unit tests, as they show working examples.