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


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
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 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 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..

Post a Comment