I had the idea to make the Razor assembly an assembly of friends on your assembly (thereby making internal visibility), but this did not work. In the Razor source code, we can see the following code (in CompilerServiceBase.cs ):
if (modelType != null) { if (CompilerServices.IsAnonymousType(modelType)) { type.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(HasDynamicModelAttribute)))); } }
And then later (in TemplateBaseOfT.cs ):
HasDynamicModel = GetType().IsDefined(typeof(HasDynamicModelAttribute), true);
Used later in the same file:
if (HasDynamicModel && !(value is DynamicObject) && !(value is ExpandoObject)) model = new RazorDynamicObject { Model = value }; else model = value;
So yes, your understanding is correct. In addition, we see that the visibility of internal elements is insufficient, because Razor considers any anonymous types as dynamic ( RazorDynamicObject extends DynamicObject ). You can try to fix the Razor code so that you do not consider anonymous types as dynamic if the internal elements of the contained assembly are visible.
In this case, I think that the code would have to generate a new public type that contains the same properties as the anonymous type of the model, so that the code created by Razor can create instances of this type. Alternatively, you could use the hack described here to allow methods to return instances of anonymous types.
source share