ncupper / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

AddSubtype does not return the added MetaType, but the object on which the method was called #376

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============
Assume a class hierarchy as:

class A<T>
{

}

class B<T,U> : A<T>
{

}

What steps will reproduce the problem?
1. My "API intuition (if you will)" tells me that the return value of 
MetaType.AddSubType should be the added sub type's MetaType object. I find, 
however, that it is not the added type but the object on which AddSubtype was 
called.

Basically, I expect the following test to pass:

[Test]
public void TestRuntimeTypeModelConfig()
{
    MetaType a = RuntimeTypeModel.Default.Add(typeof (A<String>), true);
    MetaType b = a.AddSubType(100, typeof (B<String, int>));

    Assert.That(b.Type == typeof(B<String,int>), Is.True);  
    Assert.That(a.Type == b.Type, Is.False);
    Assert.That(ReferenceEquals(a,b), Is.False);
}

What is the expected output? What do you see instead?

Expected:
Pass (the returned MetaType's type is equal to tyepof(B<String,int>)
Pass (a's Type and b's Type are not the same)
Pass (a and b are not the same reference)

I see instead:
Fail
Fail
Fail

What version of the product are you using? On what operating system?
2.0.0.621 (Win7/64-bit)

Original issue reported on code.google.com by mennod...@gmail.com on 3 May 2013 at 2:04

GoogleCodeExporter commented 9 years ago
I agree that this does not behave as I would have expected it. I tried chaining 
inheritance because it appeared to behave the way chainable APIs do. Instead I 
wound up creating an extension method, which seems like it is doing more work 
than it should have to and is still a little clunky, since I need access to the 
RuntimeTypeModel *and* the base type's MetaType; the end result is an 
"overload" for AddSubType which adds an applyDefaultBehavior parameter. I then 
Add the type to the RuntimeTypeModel first, add it as a subtype, and then 
return the MetaType created by the initial Add.

Original comment by jason.diorio on 19 Jun 2014 at 2:02