Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tahir 13 posts 73 karma points
    Jul 21, 2017 @ 14:32
    Tahir
    0

    Finding .net strongly types from ModelTypeAlias

    Hello guys,

    is there any api that i can use to find the .net type from document alias?

    so why i want to do this

    Actually, I am trying to override the default umbraco template controller. In the index method, i have RenderModel (and document alias), I want to create an instance of .net strongly typed class (using reflection) .

    Any idea?

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jul 21, 2017 @ 15:32
    Nicholas Westby
    0

    I'm assuming you've already created the .Net classes? If so, this doesn't sound like an Umbraco-specific question. Here's how you can instantiate a C# class based on the name of the class as a string (i.e., the document type alias): https://stackoverflow.com/questions/13952109/get-class-by-string-value

  • Tahir 13 posts 73 karma points
    Jul 21, 2017 @ 15:39
    Tahir
    0

    Thanks for replying @Nicholas.

    Let me explain.

    I want to find out the strongly typed class for an umbraco document type. I am on Umbraco version 7.6 and are using Umbraco.ModelsBuilder in LiveAppData mode. The .net classess are automatically generated by Umbraco.ModelsBuilder.

    so in controller,

    public override ActionResult Index(RenderModel model)
    {
        var documentTypeAlias = model.Content.DocumentTypeAlias;
    
        var stronglyType = GetStronglyTypedType(documentTypeAlias);
    
        // here i will use reflection to create an instance 
    }
    
    protected Type GetStronglyTypedType(string documentTypeAlias)
    {
        // here use documentTypeAlias to find out strongly typed class generated by Umbraco.ModelsBuilder
        return typeof(GenericContentDocument);
    }
    
  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jul 21, 2017 @ 15:52
    Nicholas Westby
    0

    I see. Does LiveAppData mode actually store the classes to the file system, or is it purely in memory? If it does store them on the file system, you can do this: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/78440-route-hijacking-using-models-builder-how#comment-250707

    That is, you can just specify the particular type in the action method:

    public override ActionResult Index(GenericContentDocument model)
    

    If you don't want to do that (e.g., if you have a bunch of types or if ModelsBuilder isn't storing them to the file system), you can use the reflection method I linked above.

    If you don't know the assembly used by ModelsBuilder, you can scan all assemblies for the types: https://stackoverflow.com/questions/4692340/find-types-in-all-assemblies

Please Sign in or register to post replies

Write your reply to:

Draft