3
namespace ConsoleApplication15
{
  using System;
  using Castle.DynamicProxy;

  public class Test
  {
    private SubTestClass subTestClass;

    public string Status
    {
      get
      {
        return this.subTestClass.SubStatus;
      }

      set
      {
        this.subTestClass.SubStatus = value;
      }
    }

    public int Data { get; set; }
  }

  public class SubTestClass
  {
    public string SubStatus { get; set; }
  }

  public class Program
  {
    public static void Main(string[] args)
    {
      var proxyGenerator = new ProxyGenerator();
      var testObject = proxyGenerator.CreateClassProxy<Test>();
      if (testObject.Status != null)
      {
        Console.WriteLine("Working");
      }
    }
  }
}

I have the following code and I want to set the Status default value to Empty string. When I run the following code the Status string is always Null and thrown A null exception!!

testObject.Status this shall return an empty string and not thrown an exception.

Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70

2 Answers2

2

To give auto implemented properties a default value, you'd have to do it in the constructor or something like:

[TestFixture]
public class UnitTest1
{
    [Test]
    public void TestMethod1()
    {
        var proxyGenerator = new ProxyGenerator();
        var testObject = proxyGenerator.CreateClassProxy<Test>();
        Console.WriteLine(
            testObject.Status != null 
            ? "Working" 
            : "no....");
    }
}

public class Test
{
    private SubTestClass subTestClass = new SubTestClass();

    public string Status
    {
        get
        {
            return this.subTestClass.SubStatus;
        }

        set
        {
            this.subTestClass.SubStatus = value;
        }
    }

    public int Data { get; set; }
}

public class SubTestClass
{
    public SubTestClass()
    {
        SubStatus = "";
    }
    public string SubStatus { get; set; }
}
Seany84
  • 5,526
  • 5
  • 42
  • 67
  • Hi @Seany84 thanks for the tip but the problem in my real problem the Property referenced another class I will update the code. I have thinked about your soultion before I have make this question but this will not sovle my problem – Bassam Alugili Mar 19 '14 at 09:53
  • 1
    @BassamAlugili in your question you asked: ` I want to set the Status default value to Empty string` I have tested the above code (see edited version) and it matches your question I believe. – Seany84 Mar 19 '14 at 10:02
  • 1
    @Seny84 mocked empty and not real empty the problem at the begin SubTestClass is null I have update the code to make it clear. – Bassam Alugili Mar 19 '14 at 10:04
  • @BassamAlugili I have edited my answer once more. Please let me know if there is anything missing. – Seany84 Mar 19 '14 at 10:25
  • 2
    Apart from using the constructor as Seany84 answered I cannot find any way to initialize the properties of a proxied object in Castle.DynamicProxy. Seany84's answer is a good idea; set up your constructor to initialize your fields – samy Mar 19 '14 at 10:28
0

I found a soultion for the problem with IInterceptor I can create my custom result. Thanks for help!

namespace ConsoleApplication15
{
  using System;
  using Castle.DynamicProxy;

  public class Test
  {
    private SubTestClass subTestClass;

    public virtual string Status
    {
      get
      {
        return this.subTestClass.SubStatus;
      }

      set
      {
        this.subTestClass.SubStatus = value;
      }
    }

    public int Data { get; set; }
  }

  public class SubTestClass
  {
    public string SubStatus { get; set; }
  }

  public class Program
  {
    public static void Main(string[] args)
    {
      var proxyGenerator = new ProxyGenerator();

      var testObject = proxyGenerator.CreateClassProxy<Test>(new MyInter());


      if (testObject.Status != null)
      {
        Console.WriteLine("Working");
      }
    }

  }

  public class MyInter : IInterceptor
  {
    public void Intercept(IInvocation invocation)
    {
      if (invocation.Method.ReturnType == typeof(string))
      {
        invocation.ReturnValue = string.Empty;
      }
    }
  }
}
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70