473,513 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Reflection.AssemblyBuilder: adding referenced assemblys

Hello ng,
anyone knowns how to add a reference to an assembly to
System.Reflection.AssemblyBuilder?

In System.Web.Compilation.AssemblyBuilder is a function like
AddAssemblyReference, but not in System.Reflection.

Regards,
Michael
Sep 11 '08 #1
9 4148
Didn't you ask this a few hours ago?
Sep 11 '08 #2
yeah, but it was deleted somehow
"Marc Gravell" <ma**********@gmail.comschrieb im Newsbeitrag
news:OQ****************@TK2MSFTNGP02.phx.gbl...
Didn't you ask this a few hours ago?

Sep 11 '08 #3
Nope - still there... I get it both from news.microsoft.com and from
google groups (watch for wrap...)

http://groups.google.com/group/micro...817db409dfbc84

Marc
Sep 11 '08 #4
oh, just found your answer. funny, my orginal entry has been deleted, but
your answer ist there.

yes, i need to i guess. the problem is, that i create an interface there,
that inherits from some other interface.

if both interfaces a created in the dynamic assembly all works fine.
if the base interface is created in some other assembly it wont show up in
the class viewer as base type of my dynamic interface.
even more weird: i can create an object of the type of the dynamic
interface, query the implemented interfaces an it shows the dynamic
interface. but the cast fails.
"Marc Gravell" <ma**********@gmail.comschrieb im Newsbeitrag
news:OQ****************@TK2MSFTNGP02.phx.gbl...
Didn't you ask this a few hours ago?

Sep 11 '08 #5
OK, here's a complete example that declares an interface in one
dynamic assembly, implements it in another, and then tests the
interface via reflection. It also saves both assemblies to disk so you
can load them in reflector/ildasm and verify that the references have
been set correctly (which they have, for me at least...):

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace ConsoleApplication3
{
class Program
{
static void Main()
{
AssemblyBuilder assembly =
AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName("Foo"),
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder module =
assembly.DefineDynamicModule("Foo.dll");
TypeBuilder tb = module.DefineType("IFoo",
TypeAttributes.Interface | TypeAttributes.Abstract |
TypeAttributes.Public);
PropertyBuilder prop = tb.DefineProperty("Message",
PropertyAttributes.None, typeof(string), Type.EmptyTypes);
MethodBuilder method = tb.DefineMethod("get_Message",
MethodAttributes.Public | MethodAttributes.HideBySig
| MethodAttributes.NewSlot | MethodAttributes.Abstract
| MethodAttributes.Virtual, CallingConventions.HasThis,
typeof(string), Type.EmptyTypes);
prop.SetGetMethod(method);
Type ifoo = tb.CreateType();
assembly.Save("Foo.dll");
assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName("Bar"),
AssemblyBuilderAccess.RunAndSave);
module = assembly.DefineDynamicModule("Bar.dll");
tb = module.DefineType("Bar", TypeAttributes.Class |
TypeAttributes.Sealed | TypeAttributes.Public);
tb.DefineDefaultConstructor(MethodAttributes.Publi c);
tb.AddInterfaceImplementation(ifoo);
MethodInfo interfaceMethod =
ifoo.GetMethod("get_Message");
method = tb.DefineMethod("MyMethodImpl",
MethodAttributes.Private | MethodAttributes.HideBySig
| MethodAttributes.NewSlot | MethodAttributes.Final |
MethodAttributes.Virtual | MethodAttributes.SpecialName
, CallingConventions.HasThis, typeof(string),
Type.EmptyTypes);
ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldstr, "hello world");
il.Emit(OpCodes.Ret);
tb.DefineMethodOverride(method, interfaceMethod);
Type bar = tb.CreateType();
assembly.Save("Bar.dll");

typeof(Program).GetMethod("Test").MakeGenericMetho d(ifoo,
bar).Invoke(null, null);
}

public static void Test<TInterface, TClass>()
where TInterface : class
where TClass : class, TInterface, new()
{
// create an instance
TClass obj = new TClass();

// check the cast (already validated by generic
constraint)
TInterface i = obj;

// try to read the property
PropertyInfo prop =
typeof(TInterface).GetProperty("Message");
object val = prop.GetValue(obj, null);
Console.WriteLine(val);
}
}
}

Sep 11 '08 #6
My main thoughts: are you calling AddInterfaceImplementation /
DefineMethodOverride? These are the important bits for applying the
interface implementation.

Also - sorry if the code (previous post) is ugly; it largely goes with
the turf for Reflection.Emit, I'm afraid...

Marc
Sep 11 '08 #7
hi marc,
i adjusted your example to meet my case. the cast works fine, there might be
some other issue in my code, but still i cant see IFoo/Foo as BaseType in
the ClassViewer.

using System;

using System.Reflection;

using System.Reflection.Emit;

namespace ConsoleApplication3

{

public interface IFoo

{

}

public class Foo : IFoo

{

}

class Program

{

static void Main()

{

AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new
AssemblyName("Bar"), AssemblyBuilderAccess.RunAndSave);

ModuleBuilder module = assembly.DefineDynamicModule("Bar.dll");

TypeBuilder tb = module.DefineType("IBar", TypeAttributes.Interface |
TypeAttributes.Abstract | TypeAttributes.Public);

tb.AddInterfaceImplementation(typeof(IFoo));

Type iBar = tb.CreateType();

tb = module.DefineType("Bar", TypeAttributes.Public | TypeAttributes.Class,
typeof(Foo));

tb.AddInterfaceImplementation(iBar);

Type bar = tb.CreateType();

assembly.Save("Bar.dll");

typeof(Program).GetMethod("Test").MakeGenericMetho d(iBar, bar).Invoke(null,
null);

}

public static void Test<TInterface, TClass>() where TInterface : class where
TClass : class, TInterface, new()

{

TClass obj = new TClass();

TInterface i = obj;

}

}

}
Sep 11 '08 #8
I tried your code; it looks fine in reflector, so I imagine that VS is
just having a hard time resolving the interface back to the current
project assembly (or something).

As long as it works, I'd stop digging
[else it might stop working again!]

Marc
Sep 11 '08 #9
well, its only working in this tiny example. not in my app wich is far mor
complicated. i guess i will have to narrow this down.
thanks for your time and help!
"Marc Gravell" <ma**********@gmail.comschrieb im Newsbeitrag
news:uK**************@TK2MSFTNGP02.phx.gbl...
>I tried your code; it looks fine in reflector, so I imagine that VS is just
having a hard time resolving the interface back to the current project
assembly (or something).

As long as it works, I'd stop digging
[else it might stop working again!]

Marc

Sep 11 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1455
by: Norm Dotti | last post by:
I'm running an app through auto-deployment ( ex. http://localhost/SampleSolution/SampleProjMain/bin/Main.exe ). I'd like to display a list of loaded assemblies, so I'm stepping through an array of...
0
2162
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using...
0
1162
by: Björn Jansson | last post by:
Hi, I have made a "shell" for Windows applications. The shell contains auto update, authentication and other things that are common for applications. The shell can contain 1 or more applications....
1
3029
by: Matthias Klöpper | last post by:
Hi there, I'm currently trying to call some API-Functions via Reflection.Emit since I need to bind to different dlls based on user input. The dynamic creation of the required PInvoke-Methods works...
1
6614
by: melanieab | last post by:
Hi again, I'm trying to programatically sort a datagrid. I did find the following code, and it does work, but, when a column header is clicked, the data only sorts in descending order. Clicking...
3
1210
by: René P. | last post by:
Hello, i have a little application that search into a special folder to assemblys. Founded assembly would be load by Assembly.LoadFrom(). Is there a way to load only assemblys that are from my...
0
1465
by: treefrog | last post by:
Greetings, I have a C# application that uses Reflection to scan through a directory, ./plugins/, for assemblies that have been assigned the PluginAttribute and loads them. This part works...
17
2291
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
1
1827
by: Michael Sander | last post by:
Hello ng, anyone knowns how to add a reference to an assembly to System.Reflection.AssemblyBuilder? In System.Web.Compilation.AssemblyBuilder is a function like AddAssemblyReference, but not in...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.