473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to create a new object.

Hello,

string typename = obj.GetType().FullName;
where obj is a object of any type.

Now my problem is how to create back the object just by passing this typename.

Thanks for the help.

Best Regards,
--
Gana
Nov 16 '05 #1
5 1651
In the reflection Namespace there's a calls called Activator. On that
class you use the method CreateInstance
--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'

gana wrote:
Hello,

string typename = obj.GetType().FullName;
where obj is a object of any type.

Now my problem is how to create back the object just by passing this typename.

Thanks for the help.

Best Regards,

Nov 16 '05 #2
Thank you. But I have FullName of the Type not the assembly. In Activator
class I need to pass either Type or FullName of the assembly.
So How to do this?

"Patrik Löwendahl [C# MVP]" wrote:
In the reflection Namespace there's a calls called Activator. On that
class you use the method CreateInstance
--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'

gana wrote:
Hello,

string typename = obj.GetType().FullName;
where obj is a object of any type.

Now my problem is how to create back the object just by passing this typename.

Thanks for the help.

Best Regards,

Nov 16 '05 #3
You have the full name of the type, but you don't know what assembly its in? If thats the case then you are stuck, consider this code

Assembly 1
namespace Richard
{
class Hello
{
public void Foo()
{
}
}
}

Assembly 2
namespace Richard
{
class Hello
{
public void Bar()
{
}
}
}

now I write the code in my application assembly

Activator.CreateInstance(Type.GetType("Richard.Hel lo));

So the question is, which class is it meant to create, the one from assembly 1 or the one from assembly 2? Thats why you need the assembly name.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Thank you. But I have FullName of the Type not the assembly. In Activator
class I need to pass either Type or FullName of the assembly.
So How to do this?
Nov 16 '05 #4
"gana" <ga**@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
Thank you. But I have FullName of the Type not the assembly. In Activator
class I need to pass either Type or FullName of the assembly.
So How to do this?


So, don't save just the name--- Save the Type.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 16 '05 #5
Hello,
Thank you. My first option was to store the type itself. But I coudnt
Serilaize the object which has a public field of type System.Type. I was
getting the exception with Messgae" There was an error reflecting type
classname". If I store the fullName instead of Type itself it worked fine.
Will loading all the assemblies before trying to create the object cause any
performance issues?

Gana.

"James Curran" wrote:
"gana" <ga**@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
Thank you. But I have FullName of the Type not the assembly. In Activator
class I need to pass either Type or FullName of the assembly.
So How to do this?


So, don't save just the name--- Save the Type.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 16 '05 #6

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

Similar topics

2
by: brazilnut52 | last post by:
I am going to outline the steps I go through to produce the problem. Hopefully this will help you understand the problem better I have created a simple COM DLL in .NET by using the COM class...
9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
2
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType();...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
2
by: Big Charles | last post by:
Hello, I would like to create an array-class to be able to call like: Dim oMyCar as New MyCar ' After initializing oMyCar, the object has to be like: oMyCar(0).Brand...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
1
by: Dave | last post by:
I have multiple forms that will create an object. Basically a energy efficiency measure object. The measure object will have a couple of required properties set but after that it can have 10-20...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.