jehugaleahsa / mustache-sharp

An extension of the mustache text template engine for .NET.
The Unlicense
306 stars 78 forks source link

mustache-sharp cannot cope with "new" properties #20

Closed nikkilocke closed 10 years ago

nikkilocke commented 10 years ago

Example: public class BaseClass { public OtherBaseClass Other { get; set; } }

public class DerivedClass : BaseClass { new public OtherDerivedClass Other { get { return (OtherDerivedClass)base.Other; } } }

If you then try to render a template for DerivedClass, PropertyDictionary will throw in line 48 at: typeCache.Add(propertyInfo.Name, propertyInfo); because there are 2 properties with the same name.

I suggest the property in the most derived class should take precedence (but just picking one at random, rather than throwing, would be better than nothing).

jehugaleahsa commented 10 years ago

Yeah. That makes sense. It will take me a while to make this change. Feel free to submit a merge request or write a failing unit test.

nikkilocke commented 10 years ago

Hi Travis,

I have a short window to have a look at fixing this. I have pulled the code from GitHub, but I am not sure how to get it to run all your tests (which I will need, if I'm going to add a new test).

Regards,

Nikki

Nikki Locke, Trumphurst Ltd. PC & Unix consultancy & programming http://www.trumphurst.com/

nikkilocke commented 10 years ago

I have temporarily created a new console app to run tests. Here is a test which fails: ///

    /// Cannot cope with two different properties of the same name in the class hierarchy
    /// </summary>

    [TestMethod]
    public void TestCompile_Two_Properties_With_Same_Name() {
        FormatCompiler compiler = new FormatCompiler();
        const string format = @"Hello, {{Other}}!!!";
        Generator generator = compiler.Compile(format);
        string result = generator.Render(new DerivedClass());
        Assert.AreEqual("Hello, DerivedClass!!!", result, "The wrong text was generated.");
    }
    public class BaseClass {
        public BaseClass() {
            Other = this;
        }

        public BaseClass Other { get; set; }
    }

    public class DerivedClass : BaseClass {
        new public DerivedClass Other {
            get { return (DerivedClass)base.Other; }
        }
    }
nikkilocke commented 10 years ago

Have done a pull request for my fix.