How to use reflection to create a class in app_code? - asp.net

I have a class Customer in app_code folder in asp.net web site, how can I create an instance using reflection, for example using Activator.CreateInstance(assemblyName, typeName)? Because the app_code is dynamically compiled, I don't know the assembly in design time?
Thanks
Fred
The question is should be how get a full name of type in design time, I want to put it in web.config. I have ConfigSection type, it is in app_code folder, I need to declare it in configSection. Thanks

You should be able to use "App_Code" or "__Code" as the assembly name in the web.config

I think you can use Assembly.GetExecutingAssembly() to get a reference to the current assembly.

I have solved a similar problem in this way:
Type[] appCodeTypes = System.Reflection.Assembly.Load("App_Code").GetTypes();

You can also use GetType().Assembly if you know that it will be the same assembly as your currently executing code.

Related

How do I call GetLocalResource in another class

So I have Test1.aspx, Test1.aspx.vb. The LocalResource files, in the App_LocalResources folder, Test1.aspx.resx and Test1.aspx.es.resx. I also have a class called TestTheData.vb in the App_Code folder.
Now what I want to do is call GetLocalResource("stringObjRes").ToString in the TestTheData.vb class. The method however is not showing up in Intellisense. When I try to type manually, I get the error lines in my code.
I've imported:
Globalization
Threading
Threading.Thread
Web
Web.UI.Page.
No luck. So how I am supposed to do this....?
Well it seems that Local Resources can't be accessed in files that are in the App_Code folder. So I used Global Resources instead.
I know it's 1 year old but I just added the comment if some others are also searching for this:
Your guess is right, you cannot access the Local Resource Object from another class. GetLocalResourceObject only exists within the code of the page, in your case Test1.aspx.vb. If you are calling the class function from your Test1.aspx.vb you could of course retrieve the Local resource from there and then supply it to your TestTheData.vb as a parameter. But if you need the 'stringObjRes' in several places (not only in Test1.aspx) then a global resource is of course preferred. Details here: http://msdn.microsoft.com/en-us/library/ms227982(v=vs.100).aspx

Assembly Information

Hi I am working in .net project.
I want to display the assembly information on the page.
User will enter the name of .net assembly in a textbox and then need to display information like all the properties, functions with parameters, constructors, destructor and inherited class names.
Please give some solution for this.
The solution is System.Reflection
using System.Reflection;
For loading assembly dynamically,use
Assembly asm=Assembly.LoadFile(#"Full path of .dll file");
Now to get all types in assembly,use
Type []alltypes=asm.GetTypes();
For information about methods, constructors,parameters,properties use
MethodInfo []GetMethods() ,ConstructorInfo []GetConstructors() ,ParameterInfo []GetParameter() ,PropertyInfo []GetProperty() etc and iterate.
And many more method are there,see the MSDN of System.Reflection

how to call an dll file dynamically from code behind(.cs)

i have an microsoft .office.interop.excel(dll) located at an directory d:\abc. now i do not want to add them as an web reference in my projet and call them
rather call the dll dynamically from my code behind(.cs)
is ther any way we can do dynmically
anyhelp would be great
thank you
Yes, but you will need to use reflection because if you don't add the assembly as reference it won't be known at compile time. Take a look at LoadFrom method.
var assembly = Assembly.LoadFrom(#"d:\abc\microsoft.office.interop.excel.dll");
var someType = assembly.GetType("Namespace.Type");
var instance = Activator.CreateInstance(type);
someType.InvokeMember(... // the reflection pain goes on
Take a look in Assembly.Load() method.
I want to discourage you from doing that. It can definitely be done if read the dll into a byte[] and call AppDomain.CurrentDomain.Load(byte[]). However you will find that you can only work with the types of that assembly through reflection. Otherwise your code behind file will not compile. So if at all possible you should add a reference (not a web reference) to the dll.

How do I access GetGlobalResourceObject function from a class that is not a page?

I have a class in my asp.net proj, I would like to get access GetGlobalResourceObject (that page exposes), from anywhere in the site, possible?
In other words I wanna access the global resources from a class that is not a page I don't care how.
Answer:
Yes, as following pseudo:
Resources.<The name of the resources file name>.<your resource key>;
Example:
lblTitle.Text = Resources.MySettings.WebsiteTitle;
Resources is an Visual-Studio auto generated namespace that exposes all the global resource classes and props in the project.
You should use
HttpContext.GetGlobalResourceObject("myResourceKey")
...because that way it will still work when using a custom ResourceProvider. The default type-generator for Resource files explicitely uses the Resx provider and won't work if you implement something like a database provider.
On some farms you'll need to wrap the call to
HttpContext.GetGlobalResourceObject("myResourceKey")
inside a try/catch block to get it over the "Could not find any resources appropriate for the specified culture or the neutral culture" error.
If you are in the site you have access to HttpContext and can use:
HttpContext.GetGlobalResourceObject("myResourceKey")
I kinda took this from the resource designer,
ResourceManager temp =
new ResourceManager("Resources.<<resource name>>",
System.Reflection.Assembly.Load("App_GlobalResources"));

SubSonic build errors

I'm trying to use SubSonic for a new project with an existing database but when I try to build the project after generating the SubSonic files I'm getting these same two exceptions on different classes:
Partial declarations of 'MyData.UserCollection' must not specify different base classes
Type 'MyData.UserCollection' already defines a member called 'UserCollection' with the same parameter types
I'm able to successfully build a new project using the Northwind DB so I believe the error has something to do with the way the tables are setup but I'm not sure where to go from there. Any help is appreciated.
Search for UserCollection in your project. SubSonic generated a partial class for this in the User.cs generated file. You either have a UserCollection of your own in which case you should probably rename it or put it in another namespace. Either that, or you tried to add functionality to the UserCollection and you derive it from another type.
Last possibility is that you have a User table and a Users table. SubSonic will change Users to User. I'm not sure, but it might cause generation errors. I haven't tried it though.
Rob,
Thanks for the help. You got me going on the right track. Apparently, the generator doesn't like tables with the word "Collection" in the name. I see now that the error was with:
public partial class UserCollectionCollection : ActiveList<UserCollection, UserCollectionCollection>
public partial class UserCollection : ActiveList<User, UserCollection>
By adding:
regexMatchExpression="Collection"
regexReplaceExpression="Group"
to the provider in my web.config file I was able to build successfully.

Resources