Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2007, 10:46 AM
Authorized User
 
Join Date: Mar 2004
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add assemby to web.config

I have created a class library and i would like to put some of the class reference in web.cofig so that i can dynamically retrieve it later thru code using reflection.

is there any idea on this or example?

thanks
 
Old September 21st, 2007, 09:14 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you explain further? How do you plan to dynamically retrieve it? What are you looking to do with it?

-Peter
 
Old September 24th, 2007, 06:38 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can add assemblies (or any other arbitrary info) under appSettings and use ConfigurationManager class to read it at run-time. Then just call Assembly.Load or Assembly.LoadFromFile to load it and Assembly.GetTypes to retrieve type information. Call Assembly.CreateInstance to create object out of types. A little example:


// Get assembly file name from config file
string asmName = ConfigurationManager.AppSettings["dynamicAssembly"];
// Load assembly
Assembly asm = Assembly.LoadFile(asmName);
// Go through every type in assembly and create instance of them
foreach (Type type in asm.GetTypes())
{
    object typeObj = asm.CreateInstance(type.FullName);
    // Go through every method in type instance and invoke them
    foreach (MethodInfo mi in typeObj.GetMethods())
    {
        mi.Invoke();
    }
}


This works with only a single assembly. That instance creation and method invoking stuff is pretty stupid but it's there just for example.

But as Planoie said, could you explain little further what you are planning to do. Reflection is a slow way to make things and depending on the case there are better alternatives.






Similar Threads
Thread Thread Starter Forum Replies Last Post
web.config dpkbahuguna ASP.NET 3.5 Basics 2 August 26th, 2008 09:41 AM
web.config vs. app.config darlo Visual Studio 2005 11 August 20th, 2008 07:23 AM
Web Config SteveP55419 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 January 3rd, 2007 04:19 PM
Web.Config tranzformerz ASP.NET 1.0 and 1.1 Basics 1 August 29th, 2005 05:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.