Generic Base Classes in ASP.Net

ASP.Net pages can inherit from custom classes (as long as they inherit System.Web.UI.Page).  This can be useful to add utility functions or shared (code-behind) behaviors to your pages.  (Note that you could also use Extension methods or HTTP modules)

However, if you try to inherit a generic base class, it won’t work:

public class DataPage<T> : Page {
    public T Data { get; set; }
}
<%@ Page Language="C#" Inherits="DataPage<string>" %>

This code results in a yellow screen of death, with the  parser error, Could not load type 'DataPage<string>'.

This happens because the ASP.Net page parser is unaware of C# generics syntax.  The familiar generics syntax (eg, List<int>) is actually a C# innovation and is not used at all in the actual framework.  The “native” generics syntax, which is used by Reflection, is markedly different: List`1[Int32] (namespaces omitted for brevity).  This name is returned by the Type.AssemblyQualifiedName property.

Since ASP.Net uses reflection APIs to load types, we need to specify generic types using CLR syntax (and with full namespaces).   Therefore, the following page will work:

<%@ Page Language="C#" 
    Inherits="TestSite.DataPage`1[[System.String, mscorlib]]" %>

However, it’s not so simple.  ASP.Net does not call Type.GetType to parse these strings; instead, it loops over every referenced assembly and calls Assembly.GetType on each one.  This is why you don’t need to include the assembly name whenever using the Inherits attribute (which would have been necessary for Type.GetType)  Ordinarily, this is very useful, but here, it comes back to bite you. 
It is not possible to parse a type from one assembly with a generic parameter from a different assembly using Assembly.GetType, unless the generic parameter is in mscorlib.

Therefore, for example, it is not possible to create an ASPX page that inherits DataPage<DataLayer.Product> if DataLayer.Product is in a different assembly than DataPage.  As a workaround, one can create a non-generic class which inherits DataPage<DataLayer.Product>, then make the ASPX page inherit this temporary class.

Next time: MVC magic

15 comments:

I recommend you to visit this website before writing thesis. Here you can learn more at least about introduction

mr jatt Mp3 New Punjabi Song,Single Tracks Latest song download also Listen Latest Music Albums Online in High Quality at Mrpendus.in
mr jatt

Yes, this is a pretty excellent topic and it is really helpful. I read various topics, but I also read your blogs. I'm seeking for Best Essay Writer, which provides the best academic work.

This comment has been removed by the author.

Hello. great post. I really liked it. I will definitely share with my colleagues from the company writemypaper.help who are professional writers and prepare term papers and essays to order. very high quality work

You can order an essay by visiting the site write my essay Essay Keeper where information on the terms of cooperation and on average prices and deadlines is provided. Each project is evaluated separately, which means that the client pays for the complexity and urgency of the work, and not for the number of pages. Based on this, we understand that in order to reduce the price, first of all, it is necessary not to postpone the order. The more time the author has to complete, the higher the quality will be, and the lower the price

Providing services to students for completing assignments of various types since 2007, qualified authors from the project https://essaykeeper.com/term-papers-for-sale have proven themselves on the positive side. You will make the right choice by entrusting the solution to the problem of writing student work to authors who have many years of experience in writing student work. We help students who really need prompt and high-quality assistance in getting an education

Your blogs are really good and interesting. It is very great and informative. Ordinarily, this is very useful, but here, it comes back to bite you separation agreement in virginia. It is not possible to parse a type from one assembly with a generic parameter from a different assembly using Assembly. I got a lots of useful information in your blog. Keeps sharing more useful blogs..

Information that is very well written. Anyone who utilizes it, including me, will find it to be valuable. I'll read more postings if you keep up the good work.Thank you for sharing. Abogado de Delitos Sexuales Fairfax VA

Your blogs are excellent and fascinating. It's fantastic and educational.dui lawyer prince william county va

I know the basic about ASP .Net about your article thank you for sharing this useful article. Dropping domestic violence charges in virginia

Thanks for shedding light on the limitations of generic base classes in ASP.Net. Looking forward to learning about MVC magic in your next post
Quick and Easy Divorce in New York

Generic base classes in ASP.NET are used to create reusable components that can work with any data type. They are defined with one or more type parameters, specified within angle brackets. When a concrete class inherits from a generic base class, the actual type(s) to be used for the type parameter(s) is specified. This allows for flexibility, reusable components, and easier maintenance. Generics provide compile-time type safety, catching errors early in development and improving code reliability. They are implemented using type erasure, resulting in efficient execution and minimal performance overhead. Generic base classes are essential for modern web development with ASP.NET fairfax gun lawyer.

Post a Comment