DOCX

Using Mono. Enough auxiliary ASP

By Sally Palmer,2015-04-04 05:22
10 views 0
Using Mono. Enough auxiliary ASP

    Using Mono. Enough auxiliary ASP.NET MVC Model

    with dynamic type

    Before this is also in the pearl river delta is one of the sample on the technique salon, solution of dynamic type is used in the ASP.NET MVC Model in a real problem.4 c # compiler support dynamic type, so the natural can write the page template view it as a type of Model.The demand of the presentation layer is easy to change, so the dynamic type of the Model can reduce the trouble we modified strong type Model, coupled with the use of anonymous types, indoor and outdoor is like a duck to water.However, if an anonymous type directly used as a Model to the view, by default, will throw an exception.We can use the Mono. Enough to change the situation.

    In view of the Model with dynamic type

    We first to reproduce the problem.Create a c # 4 ASP.NET MVC website, add the following Controller, including the anonymous types as the view Model:

    public class HomeController : Controller

    {

    public ActionResult Index( string title = " <<Default>> " )

    {

    return View( new { Title = title });

    }

    }

    And define a Index. Aspx as a view template, as dynamic Model types, with the Title:

    <% @ Page Language = " C# " Inherits = " System.Web.Mvc.ViewPage<dynamic> " %>

    <! DOCTYPE html >

    < html >

    < head runat ="server" >

    < title > Index </ title >

    </ head >

< body >

    < h1 > <% : Model.Title %> </ h1 >

    </ body >

    </ html >

    In theory, should do everything is normal, but after running will prompt

    can not find the Title on the Model members:

    What reason is this?

    Access level and members

    In c # 4, we also can construct a Model type as the view of the Model, such

    as:

    public class IndexModel

    {

    public string Title { get ; set ; } }

    Using this approach can run normally by the completely.So why specific types to work normally, and the anonymous type failed?"According to that" they are not ordinary type, and then access their properties?We use the ILSpy view using anonymous types compiled as a result, the anonymous types can be found with the above IndexModel there is one important difference:

    Because it is "anonymous types", obviously its access level should be internal, so that it can "hide".But it creates trouble for ASP.NET MVC view.Because of the ASP.NET MVC view will dynamically at runtime compilation aspx for additional DLLS, so it is unable to access to the Controller's internal members of the assembly.After the test, if we amend the IndexModel before access level to internal will get the same results.

    Additional, similar code can run through in Mono.This means that at the time of dynamic access object members, Mono and. NET in the level of access check is different.Although Mono more convenient in the scene, but in theory, the. NET approach is more reasonable.

    Use NuGet install Mono. Enough

    Mono.CecilIs one of the components of Mono, used to edit the.net assembly file.We can use it to find out a.net assemblies of internal structure, as in the case of reflection, but does not need will load in the assembly, Mono. Enough just read physical file.ILSpy was used in the above, for example, use the Mono. Enough.And, more importantly, Mono. Enough can modify and maintain the assembly, this can let us do all kinds of strange requirements.As mentioned, this article is only a profound.

    Mono and.net is binary compatible, so we can directly under the Mono Mono. Enough. DLL copy and reference to the. NET program.But to do so is in trouble, and now in. NET platform using the various components have a more convenient approach: using a package manager.A package manager is called the.net platformNuGet, it is by the author of the SubText, later hired by Microsoft for ASP.NET MVC application manager Phil Haack take the lead in the development of open source project.NuGet provides Visual Studio extension, at the same time, also have based on the PowerShell command line.Here we begin from the Visual Studio extension use.

    Create a named PublicAnonymous console project, and select the Reference - the Manage NuGet Packages:

    Search Mono. Enough, and install it:

    NuGet will automatically handle dependencies between components and configuration of the project, you can also play myself.

    Using Mono. Enough to modify the assembly

    A Mono. Enough we can modify the program set up, a few lines of code:

static void Main( string [] args)

    {

    var asmFile = args[ 0 ];

    Console.WriteLine( " Making anonymous types public for '{0}'. " , asmFile);

    var asmDef = AssemblyDefinition.ReadAssembly(asmFile, new ReaderParameters {

    ReadSymbols = true

    });

var anonymousTypes = asmDef.Modules

    .SelectMany(m => m.Types)

.Where(t => t.Name.Contains( " <>f__AnonymousType " ));

foreach ( var type in anonymousTypes)

    {

    type.IsPublic = true ;

    }

asmDef.Write(asmFile, new WriterParameters

    {

    WriteSymbols = true

    });

    }

    First of all, derives from the parameters of the need to modify the assembly name, find all the anonymous types, and its level of access to the Public after save.Save time will WriteSymbols parameter is set to true, it would also modify the PDB file - this is very important, otherwise the modified assemblies cannot and PDB file corresponding to the content, can't debugging.In other words, Mono. Enough can correctly handle the PDB file.

    In the end, as long as the compile time using this web site in ASP.NET MVC project, only need to configure it Post Build event:

Compile and run the program again, can get the right result.Get ILSpy to

check:

    conclusion

    In the salon, a friend asked me how could become a senior. NET technical personnel.I don't know how to be, but I think, understand the development situation of ecological environment, to understand. The advantages and disadvantages of.net, even can understand other fields related to the direction of the technology development trend, should be good. Characteristics of.net programmers.

    .net and Mono is an important part of the ecological environment.

Report this document

For any questions or suggestions please email
cust-service@docsford.com