Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    ASP.NET


Advertisements:










C# - Create A New Instance Of A Type Without Reflection?


I have a function that at the moment takes a Type variable. This function sticks it in a list and such and eventually needs will create a class of that type. Right now I do that with

object o=MyType.GetConstructors()[0].Invoke(new object[0]);

which is pretty hacky and also will not work in medium trust due to reflection(I think). Is there a better way of doing this without reflection? The Type is defined this way as part of a function. I need the class to be created "lazily" because it may not be created in the application if it's not needed. I use it for example like

AddToList(typeof(Whatever));

Note, I'm open to suggestions on changing the function calling. I just need the object to be created lazily and for to store the type(or however to create an object of the type) in a list.


View 3 Replies (Posted: Jan 14 at 2:02)

Sponsored Links:

Related Forum Messages For ASP.NET category:
C# - Trying To Create Instance Of Class By Using Reflection
I am trying to create instance of class by using reflection in ASP.net web site. Class ClassName is defined and located in App_code folder. Following line returns null, what could be wrong.

Type type = Type.GetType("NameSpace.ClassName", false, true);

Posted: Apr 16 10 at 22:19

View 5 Replies!   View Related
C# - Create An Instance Of A Web Control Using Reflection?
How can I create an instance of a web control at runtime using reflection? I created a series of controls that implement a common interface and I would like to create these controls based on the name of the control which is stored in my database.

I have attempted (and failed) to create an instance of these controls using Activator.CreateInstance in the following ways:

Activator.CreateInstance("MyUserControl")

and

Activator.CreateInstance("ASP","controls_myusercontrol_ascx")

...and both return null.

I've also attempted to simply get the type of the control by trying..

Type t = Type.GetType("controls_myusercontrol_ascx");

and

Type t = Type.GetType("MyUserControl");

...and it returns null.

If I explicitly declare an object as controls_myusercontrol_ascx or MyUserControl, there is no issue -- but it can't be found with reflection.

Is it possible to create web user controls using reflection at run time? If so, how can I?

Posted: Oct 20 10 at 20:59

View 1 Replies!   View Related
Web Forms :: How To Create An Instance Of A Type From Type
How would I create aninstance of a type if all I have is the type in string form?

[Code]....

Now if I wanted to create and instance of myType how would I do that ?

Posted: Feb 01, 2010 02:27 PM

View 1 Replies!   View Related
C# - Create And Instance Of MyClass Using The Type?
Bit of a puzzler, I have a generic class

public abstract class MyClass<T> : UserControl
{
}

and I have got a type like this

Type type = Type.GetType("Type From DB as String", true, true);

and I want to create and instance of MyClass using the type... But this doesn't work.

MyClass<type> control = (MyClass<type>)LoadControl("/UsercControl.ascx");

Posted: Sep 24 10 at 14:49

View 4 Replies!   View Related
C# - Creating A Generic IList Instance Using Reflection?
I am trying to create a generic list of objects using reflection. The below code throws an error Cannot create an instance of an interface. . I could change the IList to List and it works fine, but I was wondering if there is way to get this working with an IList.

var name = typeof (IList<T>).AssemblyQualifiedName;
Type type = Type.GetType(name);
var list = Activator.CreateInstance(type);

Posted: Feb 4 10 at 10:45

View 8 Replies!   View Related
.net - Generic Way Of Checking Property Type Using Reflection
I am trying to set property value using reflection as below. I wanted to know if there is any generic way of finding the property type instead of doing it int he below way.

string currentlblTxt;
string currentTxt;
Assembly assembly = Assembly.GetAssembly(typeof(myAdapter));
myAdapter currentEventObject = (myAdapter)assembly.CreateInstance(myClassName);
[code]...

Posted: Mar 25 at 15:07

View 1 Replies!   View Related
Use System.Type.InvokeMember() / Invoke Method Dynamically Using Reflection?
I understood that we can use System.Type.InvokeMember() to invoke method dynamically using Reflection, but I couldn't get proper example to show how to work on it. Can anyone please explain how to do it in
Visual Studio 2010

Just for practice purpose I have created one class called MathFunctions like below:
namespaceCSHWEB
{
public
class
MathFunctions
{
public
double
SimpleInterest(double
PrinicipalAmount, double
TimePeriod, doubleRateOfInterest)
{
return
(PrinicipalAmount * TimePeriod * RateOfInterest) / 100.0;
}
}
}

Now,
how can I invoke above method SimpleInterest with its parameters without instantiating the class?

Posted: Feb 24, 2011 01:10 AM

View 1 Replies!   View Related
Custom Server Controls :: Create Class Copy Using Reflection
Using reflection, can you do something like this?

[Code]....

Posted: Sep 22, 2009 08:47 AM

View 5 Replies!   View Related
Comparing An Instance Of Type To Type
I have a basic question about comparing the type of a variable to a specific type. If I have variable A and variable B, both an instance of a certain type, then I know I can compare the types by getting A.GetType and B.GetType, I can also use IsAssignableFrom and IsSubclassOf for more advanced things. However if I only have variable A and I want to know if variable A is of type MyCustomType, as I see it now I first have to create an instance of MyCustome Type, get the type from it and then compare it to the type of A like in the next Example:

string A = "test";
MyCustomType B = new MyCustomType();
Type typeToCompare = B.GetType();
if (A.GetType().FullName == typeToCompare.FullName) {...}

So if I need to know if A is of MyCustomType isn't there an easyer, shorter alternative that I can just try something like the following (which is ofcourse not possible):

if (A.GetType == MyCustomType....) {...}

so without first having to create an instance of the type simply to extract the type from it.?

Posted: Dec 31, 2010 08:38 AM

View 1 Replies!   View Related
C# - How To Create A Generic/parent Type That You Could Send To Methods Without Having To Define An Overload For Each Type
I'm trying to create a validation layer that will contain methods to validate all my objects (in my Business Objects layer) .. but when I try to reference both the validation and business objects to each other I get a circular dependency error .. so I've decided to create a new layer (BLL) to validate the objects for me and I'll be able to reference both the validation and the object layers.

so I want to build some kind of class/interface -I don't know what fits more- to be like a generic type or a parent type that my method could accept it as a parameter and check for it's Name/ID property. Instead of defining a new method overload for each object type I have
Simplification

Posted: Mar 5 at 18:43

View 1 Replies!   View Related
MVC :: Can't Create Strongly Type View User Controls/ Could Not Load Type 'System.Web.Mvc.ViewUserControl<MyModel>'
I inherit from ViewUserControl and it works. I then addmy model to the end <> using the full namespace path and now I get

Could not load type 'System.Web.Mvc.ViewUserControl<MyModel>'.

Doesn't matter what I use inside <>. I could set it to string, List<string>, whatever. It just won't load it anymore. Of course, in a brand new test mvc application, it does work.

Posted: Jun 05, 2009 10:01 PM

View 6 Replies!   View Related
Access Of Shared - Constant - Enum Member Or Nested Type Through An Instance
I got an error in line 9 which says, Warning1Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

[Code]....

Posted: Jan 07, 2009 04:11 PM

View 4 Replies!   View Related
JQuery ::create A 'hyperlink' Type And A 'pdf' Type?
I am struggling to find relevant documentation on this, what I basically want to do is to create a 'hyperlink' type and a 'pdf' type that I can put into my XML file and use to link to a webpage or a PDF, with the relevant icon.

Here is my JQuery :

[Code]....

And my XML :

[Code]....

Posted: Feb 28, 2011 06:42 PM

View 5 Replies!   View Related
Cannot Create New Instance Of Class
how to create a new instance of my class (say X) in another class? HWenever I try it I get the familiar error " The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)

Yet I have this class sitting in my App_Code folder. However, I am not using Visual Studio. Is it not possible to do this outside of VS, say Dreamweaver or in another IDE?

Posted: Feb 02, 2009 11:05 AM

View 2 Replies!   View Related
SQL Server :: How To Create The New Instance Along With PC Name
I have a question regading sql name instance ???

When ever i create new instance in sql server 2005 , it use to create the new instance along with my PC name.

For Eg :

My PC Name is Web and New instance name is Busiwork it use to create like WebBusiwork. But i need just the sql instance name of my server as Busiwork.

Posted: Sep 26, 2010 03:19 PM

View 5 Replies!   View Related
How To Create New Instance Of An Object
As some may be aware, I recently posted about high memory usage on my website and I have an idea that my thumbnailer may have something to do with this as I am not actively disposing of the instance when it has been used due to my misunderstanding how it works.

I am now looking at my code for the thumbnailer and would like some advice on when something would actually need disposing of, is it ONLY when you create a new instance of an object?

Like:

Target := System.Drawing.Bitmap.Create(Trunc(Width), Trunc(Height));
MyImage := Target.FromFile(PhotoPath);

So, my question would be, do I need to dispose of both Target and MyImage to make sure the GC does what it needs to do properly?

Posted: Aug 5 10 at 14:27

View 4 Replies!   View Related
C# - How To Create Instance Of A Class Only Once
A instance of a class is created in the partial class of an aspx page.Under page_load or button click method I'm trying to set the value to the class. but when each postback takes place new instance is created and I'm losing the previous value.

public partial class DatabaseSelection : System.Web.UI.Page
{
DBProperties dbpro;
Metadata obmeta;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dbpro = new DBProperties();
}

Posted: yesterday

View 4 Replies!   View Related
C# - Use String Value To Create New Instance?
I have a few classes: SomeClass1, SomeClass2.

How can I create a new instance of one of these classes by using the class name from a string?

Normally, I would do:

var someClass1 = new SomeClass1();

How can I create this instance from the following:

var className = "SomeClass1";

I am assuming I should use Type.GetType() or something

Posted: May 6 10 at 3:23

View 3 Replies!   View Related
How To Create Instance Of A Class Dynamically
I wanna do smth like this:

[Code]....

Does anyone have a recommendation how this is archievable? I am trying to pass in the Type to the method and then create an instace of it and then access the properties in it.

Posted: May 21, 2010 05:12 AM

View 8 Replies!   View Related
C# - How To Create New Instance Of A Page And Obtain URL
i'm a beginner web programmer. I've been coding desktop applications primarily. Right now, i have created this web app, in Silverlight, that uses a web service of my own for querying the database. The thing is, one of the app functionalities is the ability to open PDF files. I know that silverlight won't let you do this, but using an IFrame on top of the silverlight application you are able to display a page with de pdf file (using acrobat plug in). So here's the problem, my silverlight app passes the pdf path to the web service and, in return, the web service would create a new Page and pass the new page URI back so that it can be displayed on the IFrame:

[Code]....

Posted: Aug 6 10 at 9:37

View 1 Replies!   View Related
Create A Usercontrol Instance Programmatically?
I have a UserControl that I need to add dynamically. I tried to follow this MSDN article, but I'm not having any success....[URL]

The UserControl is basically an image gallery, and it loads some pictures based on an ID. My idea was to make this ID available as a property. Then when I create an instance of the control, I could set this ID and add it to the form.

I added a reference to the control in the .aspx page that will use it, like this:

<%@ Reference Control="~/PictureGallery.ascx" %>

And in the UserControl I added a ClassName like this:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"
Inherits="PictureGallery" ClassName="PictureGallery" %>

When I try to create an instance in the .aspx.cs like the article suggests, Dim gallery As ASP.PictureGallery, I get an "Type ASP.PictureGallery is not defined".

The article mentions a namespace, ASP, and I tried importing it to the .aspx.cs with no luck. So, I'm not able to get a reference to the UserControl.

Posted: Feb 16 at 11:57

View 3 Replies!   View Related
MVC :: Razor - Create Instance Of View?
In my controller method, I'm trying to create an instance of a View (cshtml file) before I wrap it around an ActionResult and return it.Since there is no "class" for Razor Views to speak off, how does one go about creating an instance of a Razor View?

Posted: Oct 12, 2010 03:35 AM

View 25 Replies!   View Related
C# - Cannot Create Instance Of Abstract Class?
I am trying to compile the following code and i am getting the error:

Cannot create instance of abstract class .

m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
// Create an array for the headers and add it to cells A1:C1.
object[] objHeaders = {"Order ID", "Amount", "Tax"};
m_objRange = m_objSheet.get_Range("A1", "C1");
m_objRange.Value = objHeaders;
m_objFont = m_objRange.Font;
m_objFont.Bold=true;
// Create an array with 3 columns and 100 rows and add it to
// the worksheet starting at cell A2.
object[,] objData = new Object[100,3];
Random rdm = new Random((int)DateTime.Now.Ticks);
double nOrderAmt, nTax;
for(int r=0;r<100;r++)
{
objData[r,0] = "ORD" + r.ToString("0000");
nOrderAmt = rdm.Next(1000);
objData[r,1] = nOrderAmt.ToString("c");
nTax = nOrderAmt*0.07;
objData[r,2] = nTax.ToString("c");
}
m_objRange = m_objSheet.get_Range("A2", m_objOpt);
m_objRange = m_objRange.get_Resize(100,3);
m_objRange.Value = objData;
// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book2.xls", m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

Posted: Mar 31 10 at 13:10

View 2 Replies!   View Related
How To Create Instance Of Excel On Remote Server
We need to create a fairly complex excel document on the fly for our users. Unfortunately, these aren't all internal and many customers still use versions of excel prior to 2007. Thus we can't use the xml format. (and the 2003 xml format doesn't do images)

I need to be able to instantiate excel on a different machine (so it's off our webserver) to generate binary excel files. I'm at my wits end. It just refuses to work, I keep getting error 80070005 (access denied). Yet in the dcom settings we're gone so far as to grant everyone full access and still, the same error.

Ironically, the old vb6 app has no problem calling CreateObject("Excel.Application", server).

Yet our .NET app attempts the following

[Code]....

and it fails with the error, so we tried importing the vb namespace so I could call Interaction.CreateObject and still I get the error

Has anyone had success at this? Or barring that, a way to convert a 2007 open xml excel file to a 2003 binary format on the server?

Posted: Nov 28, 2008 02:10 PM

View 3 Replies!   View Related
How To Create Multi Instance Application In Azure
I want to create multiinstance application in Azure. and then demontrate that if one of the instance is down other instance is working fine. How shouldI achive that.

Posted: Feb 06, 2010 07:42 AM

View 1 Replies!   View Related
Cannot Create An Instance Of A COM Component On A Server Side
I'm trying to use MS IME on a server to retrieve some Japanese info in a silverlight app. The app accesses to the server by using WCF, but when calling a CoCreateInstance to create a IFELanguage2, it gives me -2147467262. The same code I'm using on the server actually works pretty fine on a WPF app.

Here is the code,

// ...omit
Guid imeGuid;
int errCode = Ole32.CLSIDFromString("MSIME.Japan", out imeGuid);
WinBase.CheckError(errCode);
Guid feLangIID = new Guid(Constants.IID_IFELanguage2);
IntPtr ppv;
errCode = Ole32.CoCreateInstance(imeGuid, IntPtr.Zero, Ole32.CLSCTX.CLSCTX_ALL, feLangIID, out ppv);
WinBase.CheckError(errCode); // errCode is 2147467262
IFELanguage IfeLanguage = Marshal.GetTypedObjectForIUnknown(ppv, typeof(IFELanguage)) as IFELanguage;
errCode = IfeLanguage.Open();
WinBase.CheckError(errCode);
// ...omit

Do I need to setup anything to use the MS IME on a server side? I'm running the app on the following environment, IIS7 .NET Framework 4.0 Windows 7 pro 64 bit VS2010

Posted: Jul 20 10 at 2:44

View 1 Replies!   View Related
MVC :: Cannot Create An Instance Of The Abstract Class Or Interface?
Error 1279 Cannot create an instance of the abstract class or interface 'System.Web.Mvc.FileResult'

[Code]....

I am using MVC 2. The same code works in my onather application. I have no idea about this error.

Posted: Dec 12, 2010 07:51 PM

View 2 Replies!   View Related
MVC :: System.MissingMethodException: Cannot Create An Instance Of An Interface
I have a controller that is used for searching: searchController. It takes user input, searches the model for it and then forwards the result to another controller that then displays the results.

Search Controller:

[Code]....

Second Controller used for displaying the information:

[Code]....

From my attempts at debugging I thinka problem lies with the redirect but I'm having a hard time figuring out what is wrong because the error message I get is unknown to me: System.MissingMethodException: Cannot create an instance of an interface. That message is displayed rightwhen redirect is called.

Maybe I can't pass an IQueryable between controllers?

Posted: Sep 06, 2009 06:26 AM

View 2 Replies!   View Related
Create Instance Of Class And Pass From Web Form - Resource
How do I create an instance of a class and pass it from one web form to another. Is there a resource which explains the code in the time tracker starter kit?

Posted: Jul 13, 2009 09:54 PM

View 3 Replies!   View Related
DataSource Controls :: How To Create A New Instance In SQL Server 2005
I have installed VS 2008 recently and also installed SQL Server Express 2005 from the package. Everything installed successfully, but when I lanunch SQL ServerManagement Studio, I do not see any user instance, NOT EVEN localhost, in Connect To Server dialog box in the Server Name DropDown. Because of that, I am not able to create or add new database. Might be I have skipped some configuration steps. How do I create a new instance in SQL Server Express 2005?

Posted: Mar 30, 2009 06:14 AM

View 5 Replies!   View Related
SQL Server :: XML Data Type / All The Characters With Accent Are Replace By A Question Mark, For Instance Get "Au Secours Des Coll?ges"?
I want to store into my xml data type column some french characters with accent e.g "é, à, è". The header of my xml file use the encoding style encoding="ISO-8859-1"

My file is the follow :

<?xml version="1.0" encoding="ISO-8859-1"?>
<Magazine DateParution="13-03-2011" Numero="150">
<Title> La chutte du président Osni Mobarack</Title>
<Articles>
<Titre_Article>
Au secours des collèges
</Titre_Article>
<Note_Article>
L'article montre la capitale...</Note_Article>
<Page_Article>1</Page_Article>
</Articles>
</Magazine>

When i store this data into my database using Insert into statement, all the characters with accent are replace by a question mark. for instance get "Au secours des coll?ges"

What can i do in order to SQL Server accept the international characters like french's

Posted: Mar 21, 2011 12:29 AM

View 2 Replies!   View Related
Web Forms :: Trying To Create An Instance Of System.Web.HttpResponse To Send The Uers To Another Website?
I am trying to create an instance of System.Web.HttpResponse to send the uers to another website.I am doing some other stuff in my vb.net that does not let me use the regular Response.Redirect.when I do Dim myRsp As System.Web.HttpResponse = New System.Web.HttpResponseit complains about a textwriter.

Posted: Jul 11, 2010 05:50 PM

View 4 Replies!   View Related
Forms Data Controls :: Create An Instance Of A TextBox In A GridView When Using FindControl?
In my GridView I use FindControl/<FooterTemplate> with a TextBox that you can enter an integer with a comma

into the TextBox, because I create an instance of the Textbox first and then remove the comma out of the integer before the TextBox is used to update the table in the database.

However once the data is in the table and displayed in the GridView if you try to change the integer and use a comma in the GridView you cannot because FindControl/<EditItemTemplate> will not create an instance for you to remove the comma from the integer before it updates the table because FindControl/<EditItemTemplate> doesn't allow you to create an instance of the TextBox first, instead when you try to use the instance that looks like was created it gives an error stating:

Object reference not set to an instance of an object.

So the question is:

HOw do you create an instance of a TextBox in a GridView when using FindControl/<EditItemTemplate>?

<form id="form1" runat="server">
<div>
</div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

[Code]....

Posted: Dec 28, 2010 09:44 PM

View 2 Replies!   View Related
Visual Studio :: Vs Asp2008 Instance Cant Recognize SqlExpress 2008 Instance On Machine
vs asp2008 instance cant recognize the sqlExpress 2008 instance on my machine

I have wonder if you could try to help me with the fallowing issue:When trying to add a new sql server connection (local or remount) from VS(visual studio) 2008 express - on the add connection dialog

box: server name: are Empty!!
The vs couldn't point to any database, like dont recognize the sqlExpress 2008 instance i got on my machine.

1. Have tried to "play" with sa property's(from sqlS: Databases: security: login tree folder) and with the sql

s windows service: stop and restart
2. And so with the target db.
3.

On the services.msc
The sql server express the status is started.
The sql server agent status is disabled and don't have the option to start.
The sql server explorer status is disabled and don't have the option to start.

4. And so reinstalled the asp.

Posted: Feb 06, 2010 09:34 AM

View 2 Replies!   View Related
Architecture :: Singleton With Instance Methods - Instance Is Unique In Each AppDomain Or Across AppDomains?
A singleton can have only one instance in memory.

1. Instance is unique in each AppDomain or across AppDomains?
2. I have two Virtual Dirs, and I am using same class. Howmany instances would be there?
3. If three users are accessing same method from Singleton class, then each will provide difference output. So internally .NET creates Instances of these methods? e.g. A method gets NAME as input param and returns "Welcome NAME !". If three users are accessing same method as same time, then output will be 'WelCome NAME1', 'WelCome NAME2'...NAME3. How it is done?

Posted: Dec 19, 2007 04:49 AM

View 10 Replies!   View Related
C# - Best Way To Get Only Certain Properties Using Reflection?
I am trying to come up with the best way to get only certain properties from a type using reflection. How can I differentiate the properties from each other?

I understand that I can use binding flags or name. But say I want only a certain four properties. Would the best way be to create a custom attribute for the ones I want then loop through all of the properties to see if they have that attribute?

Posted: Oct 21 10 at 16:13

View 3 Replies!   View Related
C# - Set Attributes Values Using Reflection?
I have a class decorated with a attribute ...[DataEntity("MESSAGE_STAGING", EnableCaching = true, CacheTimeout = 43200)] for some requirement,I want to change this value "MESSAGE_STAGING" at run time to "Test_Message_Staging". What is the best possible way to achieve this? Can i use reflection ,Or is there any other way to do this.

Posted: Jan 29 10 at 7:18

View 3 Replies!   View Related
C# - Get All Properties For A Class Name Using Reflection?
I am loading the dll as shown below,

Type[] _Type = Assembly.GetAssembly(typeof(StdAdapter)).GetTypes();

Now I want to get all the properties for a particular 'class name' which is being passed as a string.

Posted: Mar 21 at 11:09

View 3 Replies!   View Related
Reflection - How To Get The Values Of An Object
I have an object with unknown number of properties.

I want to create a table on runtime, that shows the objectnames. I got this working via reflection.

But how do I get the values of the objectproperties?

Posted: Mar 18, 2009 08:59 AM

View 3 Replies!   View Related
Copyright 2005-08 www.BigResource.com, All rights reserved