Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / Debugging / October 2004

Tip: Looking for answers? Try searching our database.

Extending a type at runtime - debugging issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mayerber - 22 Oct 2004 02:51 GMT
Hello,

I need to extend a type at runtime in order to add some properties/fields to
it. I've managed to create the derived type with help from the
System.Reflection.Emit namespace and the code runs fine. Sample code:

namespace RefTest
{
  public class Base
  {
     private string name = "name";

     public string Name
     {
        get { return this.name; }
     }
  }

  class Class1
  {
     [STAThread]
     static void Main(string[] args)
     {
        Base o = (Base)CreateDerivedInstance();

        Console.WriteLine("Name = " + o.Name);
     }

     public static object CreateDerivedInstance()
     {
        AssemblyName assemblyName = new AssemblyName();
        assemblyName.Name = "MyDummyAssembly";
        AppDomain domain = Thread.GetDomain();
        AssemblyBuilder assemblyBuilder =
domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder module =
assemblyBuilder.DefineDynamicModule("MyDummyModule", "MyModule.dll", true);
        TypeBuilder typeBuilder = module.DefineType("Derived",
TypeAttributes.Public, typeof(Base));
        ConstructorBuilder cb =
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

        // This type extends type Base
        Type derived = typeBuilder.CreateType();

        return Activator.CreateInstance(derived);
     }
  }
}

As you can see, in Main() I declare local variable 'o' of type Base but at
runtime it is actually an instance of type 'Derived'. The code runs fine. The
problem is that when I set a breakpoint and add 'o.Name' to the Watch window
I get "error: 'o.Name' does not exist" in the Value column. That is, I lose
debugging information about the variable even for members of the base class
(the 'Name' property).

Could anyone shed some light on this? Is there a workaround?

I'm using VS 2003.

Thanks in advance,
mayerber.
Yama - 28 Oct 2004 22:39 GMT
Hi Mayerber,

At what moment does o.Name break? hen you drag and drop the o object into
the Watch list, right after it is initialized, what do you see below? Does it
create a tree like of the object you can browse into?

Yama Kamyar

> Hello,
>
[quoted text clipped - 59 lines]
> Thanks in advance,
> mayerber.
mayerber - 30 Oct 2004 22:06 GMT
Hello Yama,

It breaks right away. When I drag and drop the 'o' variable to the Watch
window, I get "{}" for the Value column. It creates absolutely no tree to
browse. It's like the debugger completely loses all type information/symbols
about the object, including the symbols declared in the Base class (the Name
property, etc.).

Perhaps there's something that I must do in the CreateDerivedInstance()
method that helps the debugger with type information/symbols. I don't know...

Thanks for your interest,
mayerber.

> Hi Mayerber,
>
[quoted text clipped - 67 lines]
> > Thanks in advance,
> > mayerber.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.