CodeVerge.Net Beta
Login Idy
Register Password
  Forgot?
   Explore    Item Entry    Profile    Invite   History    Resources   SiteMap  
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML




Zone: > newsgroup > asp.net forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 8/11/2002 8:58:36 AM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 9 Views: 9 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
10 Items, 1 Pages 1 |< << Go >> >|
CreProDes
Asp.Net User
Need help in Loading a assembly dynamically8/11/2002 8:58:36 AM

0/0

Hi all,
Good day. I have trouble in loading my custom controls. The plot is
like this, the namespace and the class name will be given in a database when
loading the page (fully pagelet) i want to use this settings from the
database and load the control into the page. I couldnt do this using
Assembly.LoadFrom and CreateInstance methods because the CreateInstance
returns a Object and I cant add this as simply using Page.Controls.Add. I
came across this LoadControl metod but it gets the path of a ascx file as
opposed to a custom server control what iam trying to load. Please help me
in this.

What iam doing as code :

<%@ Page Language="C#" %>
<script runat="server">
public void Page_Load(Object sender,EventArgs e) {
System.Reflection.Assembly asm =
System.Reflection.Assembly.LoadFrom(Page.MapPath("/bin/MyAssembly.dll"));
Control gc = asm.CreateInstance("MyControl"); // Get error, since it cannot convert Object to Control //
Page.Controls.Add(gc);
}
</script>
<html>
<head>
<title>Test Dynamic Control Loading</title>
</head>
<body>

</body>
</html>

Thanking you in advance.

CreProDes
Brian Bilbro
Asp.Net User
Re: Need help in Loading a assembly dynamically8/11/2002 4:53:47 PM

0/0

If you know that "MyControl" is a Control then just cast it as a control:

Control gc = (Control) asm.CreateInstance("MyControl");


Or if you want to make sure it's a control do this:

Control gc = asm.CreateInstance("MyControl") as Control;

if(gc!=null)
{
// It's a control
Page.Controls.Add(gc);
}


HTHs,
Brian


CreProDes
Asp.Net User
Re: Need help in Loading a assembly dynamically8/12/2002 5:50:33 AM

0/0

Hi,

Thanks a lot friend. It works fine now. But how do i add Attributes which are specific to my control?. Thank you.

CreProDes
G Andrew Duthie
Asp.Net User
Re: Need help in Loading a assembly dynamically8/12/2002 6:20:20 AM

0/0

If by "Attributes" you're referring to Properties that are specific to your control, then you should cast the object to your control's type, rather than Control. Then your properties will be available using the standard ControlName.PropertyName syntax.

Instead of:

Control gc = (Control) asm.CreateInstance("MyControl");

use:

TheControl gc = (TheControl) asm.CreateInstance("MyControl");

where "TheControl" is the class name of the specific control you're working with.

hth,

G. Andrew Duthie


My Blog

Code Camps in the Mid-Atlantic
CreProDes
Asp.Net User
Re: Need help in Loading a assembly dynamically8/12/2002 7:11:10 AM

0/0

Hi,

Thank you for the reply. But the answer you have given takes us to the square one of the problem. I will be loading the Path of the Assembly, Namespace and ClassName from the database (because we are using a h*ll lot of custom server controls and they want it to adjust the order in which they appear on the page as per thier wish). So i cannot say "(TheControl)" because i will not be knowing which is going to be "TheControl". Iam going through the reflection related webpages for reference, I think we can accomplish this using something of GetAttributes() collection from Type class ... working on that one. Any help would be appriciated. Thank you.

CreProDes
G Andrew Duthie
Asp.Net User
Re: Need help in Loading a assembly dynamically8/12/2002 7:28:05 AM

0/0

Sorry...guess I jumped the gun on that answer. Have you tried the GetType method? The documentation here looks like it might be promising for what you're trying to accomplish.

hth,

G. Andrew Duthie

My Blog

Code Camps in the Mid-Atlantic
Brian Bilbro
Asp.Net User
Re: Need help in Loading a assembly dynamically8/12/2002 7:30:07 AM

0/0

You can use reflection to do this. Here is what the code could look like:

Control gc = asm.CreateInstance("MyControl") as Control;

if(gc!=null)
{
// It's a control
Page.Controls.Add(gc);

Type gcType = gc.GetType();

PropertyInfo[] gcProperties = gcType.GetProperties(BindingFlags.Public|BindingFlags.Instance);

for(int i=0;i<gcProperties.Length;i++)
{
if(gcProperties[i].Name.ToLower()=="myproperty")
{
object tempObject = "SomeValue";
gcProperties[i].SetValue(gc,Convert.ChangeType(tempObject, gcProperties[i].PropertyType),null);
}
}
}



I haven't run this code but you should get the idea. You need to include the System.Reflection namespace too.

HTHs,
Brian
CreProDes
Asp.Net User
Re: Need help in Loading a assembly dynamically8/12/2002 7:42:14 AM

0/0

Hi,
offff thanks a lot, i solved my problem atlast. Yeah i used the Type class and accomplised the task something like this

gc.GetType().GetProperty("ThemeColor").SetValue(gc,"#dd1111",null);

and works fine ... thank you a lot for both Brian and Andrew. Bye.

CreProDes
bbfmh
Asp.Net User
Re: Need help in Loading a assembly dynamically11/1/2003 1:20:08 AM

0/0

But I got the Access Denied error message on the dll file when I run it in the server
System.IO.FileLoadException: Access is denied
I've place Full Control for Everyone on the file already... however, when I add the user into the Administrators group, I can successfully access the dll file from System.Reflection.Assembly.LoadFrom..... what can I do???
mhp
Asp.Net User
Re: Need help in Loading a assembly dynamically11/3/2003 9:08:01 PM

0/0

You may need permission to use reflection. If you are able to run on your local computer, problem might be related to .Net security.

If it is the problem, welcome to the world of assert, demand, permission and lot more.
-MHP
10 Items, 1 Pages 1 |< << Go >> >|


Search This Site:

Other Resources:

Newbie [Need Help] - microsoft_downloads.member_management_component ... load with generated always... it modifies the register of field ... <add assembly="MemberRole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7c773fb104e7562" ...
Need help with CSS layout - macromedia.dreamweaver - Web Programming ... ... Dynamic Drive Forums : In need of some help with my CSS for my layout CSS ... need help ?...could not load assembly. installing a web project ...
HowTo: dynamically load an assembly from the GAC So, I need the long name of the assembly in order to load from the GAC. ... there to see if it help. On Jun 3, 4:37 pm, Thomas W. Brown ...
SQL Server 2005: CLR Integration : Dynamic Assembly Loading Trick question: dynamic assembly loading is always disallowed under SQL ... Either way, you then need to register the XmlSerializers assembly in your database: ...
Hosting Open Forum Re: Need help in Loading a assembly dynamically. Re: Need help in Loading a assembly dynamically ... Need help in Loading a assembly dynamically. Need help in ...
Dynamic assembly loading Dynamic script-assembly loading ... is loaded into current AppDomain because there is no need to do any cleanup. ... created with a constructor receiving the ...
Suzanne Cook's .NET CLR Notes : Debugging Assembly Loading Failures RE: Debugging Assembly Loading Failures. Hi I need help please. ... We are Dynamically creating Assemblies and loading them in a seperate app space. ...
MSTest not binding to dynamic loaded assembly - MSDN Forums ... you have a nice simple repro, it would help greatly! SDE ... You just need to get all the dlls it needs somewhere in the defined probing path, or in the GAC. ...
Loading XAML files Dynamically : The Official Microsoft Silverlight Site I've tried embedding a xaml file in my assembly and using System.Uri and System. ... help me. Regards, Sakthi Sai Saranyan. sakthisaisaranyan. Loading...





 
All Times Are GMT