[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Create class from name

Peter Larsen [CPH]

7/8/2008 2:56:00 PM

Hi,

I can't remember how to write the code (in designtime) that constructs a
"unknown" class. I only have its base class - in designtime - not the exact
class.

The following code shows a sample of what i want to do:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

C1 c2 = CreateClass("C2");
C1 c3 = CreateClass("C3");
}

private C1 CreateClass(string classname)
{

return CreateTheClass(classname);
}
}

class C1
{ }
class C2 : C1
{ }
class C3 : C1
{ }

Please tell me how the method CreateClass() should look like.
Thank you in advance.

BR
Peter


10 Answers

Jeroen Mostert

7/8/2008 3:51:00 PM

0

Peter Larsen [CPH] wrote:
> I can't remember how to write the code (in designtime) that constructs a
> "unknown" class. I only have its base class - in designtime - not the exact
> class.
>
Activator.CreateInstance() should serve (almost) all your needs for dynamic
creation.

--
J.

Ilya Albrekht

7/8/2008 3:54:00 PM

0

C1 c1 = new C1();

On Tue, 08 Jul 2008 10:56:21 -0400, Peter Larsen [CPH]
<PeterLarsen@community.nospam> wrote:

> Hi,
>
> I can't remember how to write the code (in designtime) that constructs a
> "unknown" class. I only have its base class - in designtime - not the
> exact
> class.
>
> The following code shows a sample of what i want to do:
>
> public partial class Form1 : Form
> {
> public Form1()
> {
> InitializeComponent();
>
> C1 c2 = CreateClass("C2");
> C1 c3 = CreateClass("C3");
> }
>
> private C1 CreateClass(string classname)
> {
>
> return CreateTheClass(classname);
> }
> }
>
> class C1
> { }
> class C2 : C1
> { }
> class C3 : C1
> { }
>
> Please tell me how the method CreateClass() should look like.
> Thank you in advance.
>
> BR
> Peter
>
>



--
Using Opera's revolutionary e-mail client: http://www.opera...

Ilya Albrekht

7/8/2008 3:55:00 PM

0

Or you can use Activator.CreateInstance

On Tue, 08 Jul 2008 10:56:21 -0400, Peter Larsen [CPH]
<PeterLarsen@community.nospam> wrote:

> Hi,
>
> I can't remember how to write the code (in designtime) that constructs a
> "unknown" class. I only have its base class - in designtime - not the
> exact
> class.
>
> The following code shows a sample of what i want to do:
>
> public partial class Form1 : Form
> {
> public Form1()
> {
> InitializeComponent();
>
> C1 c2 = CreateClass("C2");
> C1 c3 = CreateClass("C3");
> }
>
> private C1 CreateClass(string classname)
> {
>
> return CreateTheClass(classname);
> }
> }
>
> class C1
> { }
> class C2 : C1
> { }
> class C3 : C1
> { }
>
> Please tell me how the method CreateClass() should look like.
> Thank you in advance.
>
> BR
> Peter
>
>



--
Using Opera's revolutionary e-mail client: http://www.opera...

v-lliu

7/9/2008 3:46:00 AM

0

Thanks Ilya and Jeroen for your replies!

Hi Peter,

As Ilya and Jeroen have suggested, a solution to create an instance from a
given class name is to call Activator.CreateInstance method. For example:

namespace WindowsApplication1
{
class C1
{ }
class C2 : C1
{ }
class C3 : C1
{ }

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// pass the full name of the class
C1 c2 = CreateClass("WindowsApplication1.C2");
C1 c3 = CreateClass("WindowsApplication1.C3");
}

private C1 CreateClass(string classname)
{
ObjectHandle objh = Activator.CreateInstance(null, classname);
C1 obj = objh.Unwrap() as C1;
return obj;
}
}

Alternatively, you can get the Type from the given class name first and
then call the TypeDescriptor.CreateInstance method to create an instance of
the type. For example:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// pass the full name of the class
C1 c2 = CreateClass("WindowsApplication1.C2");
C1 c3 = CreateClass("WindowsApplication1.C3");
}

private C1 CreateClass(string classname)
{
C1 obj = null;
Type t = Type.GetType(classname, false);
if (t != null)
{
obj = TypeDescriptor.CreateInstance(null, t, null, null) as
C1;
}
return obj;
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/de....
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Peter Larsen [CPH]

7/9/2008 9:39:00 AM

0

Thank you all for your comments and thank you to you, Linda, for your great
samples.
You did it again :-)

BR
Peter


"Linda Liu[MSFT]" <v-lliu@online.microsoft.com> wrote in message
news:sZWuQZX4IHA.4056@TK2MSFTNGHUB02.phx.gbl...
> Thanks Ilya and Jeroen for your replies!
>


Bret Cahill

5/18/2012 4:30:00 AM

0

> >>> Santorum jabbed Romney, portraying him as a coldly calculating "C.E.O"
> >>> and not the inspirational leader he said the country needed.
>
> >> It's Obvious the lying SOB, Obama is in trouble.
>
> > "I'm not familiar precisely with exactly what I said, but I stand by what
> > I said whatever it was."
>
> >    Mittens, on today's campaign trail
>
> If he doesn’t know, what should the voters know about what to expect?

Voters expect Obama to be a 2 termer.



Sid9

5/18/2012 11:23:00 AM

0


"Bret Cahill" <BretCahill@peoplepc.com> wrote in message
news:8cefb2c3-7634-4dc1-9aa4-67c54af9abc8@pr3g2000pbb.googlegroups.com...
>> >>> Santorum jabbed Romney, portraying him as a coldly calculating
>> >>> "C.E.O"
>> >>> and not the inspirational leader he said the country needed.
>>
>> >> It's Obvious the lying SOB, Obama is in trouble.
>>
>> > "I'm not familiar precisely with exactly what I said, but I stand by
>> > what
>> > I said whatever it was."
>>
>> > Mittens, on today's campaign trail
>>
>> If he doesn?t know, what should the voters know about what to expect?
>
> Voters expect Obama to be a 2 termer.
>
>
>

Obama's enemy is complacency.

clairbear

5/18/2012 3:48:00 PM

0

"Sid9" <sid9@ bellsouth.net> wrote in news:jp5bei$m7v$1@dont-email.me:

>
> "Bret Cahill" <BretCahill@peoplepc.com> wrote in message
> news:8cefb2c3-7634-4dc1-9aa4-67c54af9abc8@pr3g2000pbb.googlegroups.com...
>>> >>> Santorum jabbed Romney, portraying him as a coldly calculating
>>> >>> "C.E.O"
>>> >>> and not the inspirational leader he said the country needed.
>>>
>>> >> It's Obvious the lying SOB, Obama is in trouble.
>>>
>>> > "I'm not familiar precisely with exactly what I said, but I stand by
>>> > what
>>> > I said whatever it was."
>>>
>>> > Mittens, on today's campaign trail
>>>
>>> If he doesn?t know, what should the voters know about what to expect?
>>
>> Voters expect Obama to be a 2 termer.
>>
>>
>>
>
> Obama's enemy is complacency...
>
>
.....or the public waking up the fact they elected the wrong man for the
job.

Bob

5/18/2012 4:01:00 PM

0

"clairbear" <NoMore@msn.com> wrote in message
news:XnsA05778136F841clairbear@216.196.97.142...
> "Sid9" <sid9@ bellsouth.net> wrote in news:jp5bei$m7v$1@dont-email.me:
>
>>
>> "Bret Cahill" <BretCahill@peoplepc.com> wrote in message
>> news:8cefb2c3-7634-4dc1-9aa4-67c54af9abc8@pr3g2000pbb.googlegroups.com...
>>>> >>> Santorum jabbed Romney, portraying him as a coldly calculating
>>>> >>> "C.E.O"
>>>> >>> and not the inspirational leader he said the country needed.
>>>>
>>>> >> It's Obvious the lying SOB, Obama is in trouble.
>>>>
>>>> > "I'm not familiar precisely with exactly what I said, but I stand by
>>>> > what
>>>> > I said whatever it was."
>>>>
>>>> > Mittens, on today's campaign trail
>>>>
>>>> If he doesn't know, what should the voters know about what to expect?
>>>
>>> Voters expect Obama to be a 2 termer.
>>
>> Obama's enemy is complacency...
>>
> ....or the public waking up the fact they elected the wrong man for the
> job.

It appears that Romney is making the same mistake made by McCain; refusing
to make an issue of Obama's record and past associations. Remember, NEVER
believe what a politician says during a campaign. Judge him based on his
record, past associations and character.


clairbear

5/18/2012 4:21:00 PM

0

"Bob" <dalnetbob@att.net> wrote in
news:c0922$4fb6723b$4107e27c$25730@news.flashnewsgroups.com:

> "clairbear" <NoMore@msn.com> wrote in message
> news:XnsA05778136F841clairbear@216.196.97.142...
>> "Sid9" <sid9@ bellsouth.net> wrote in
>> news:jp5bei$m7v$1@dont-email.me:
>>
>>>
>>> "Bret Cahill" <BretCahill@peoplepc.com> wrote in message
>>> news:8cefb2c3-7634-4dc1-9aa4-67c54af9abc8@pr3g2000pbb.googlegroups.co
>>> m...
>>>>> >>> Santorum jabbed Romney, portraying him as a coldly calculating
>>>>> >>> "C.E.O"
>>>>> >>> and not the inspirational leader he said the country needed.
>>>>>
>>>>> >> It's Obvious the lying SOB, Obama is in trouble.
>>>>>
>>>>> > "I'm not familiar precisely with exactly what I said, but I
>>>>> > stand by what
>>>>> > I said whatever it was."
>>>>>
>>>>> > Mittens, on today's campaign trail
>>>>>
>>>>> If he doesn't know, what should the voters know about what to
>>>>> expect?
>>>>
>>>> Voters expect Obama to be a 2 termer.
>>>
>>> Obama's enemy is complacency...
>>>
>> ....or the public waking up the fact they elected the wrong man for
>> the job.
>
> It appears that Romney is making the same mistake made by McCain;
> refusing to make an issue of Obama's record and past associations.
> Remember, NEVER believe what a politician says during a campaign.
> Judge him based on his record, past associations and character.
>
>
>

Obama really only had one of those association
He had no real record in 08 and still has no character
He was the ultimate example of form over substance
As to his association few in the media really looked into aside from Fox
news The MSM did nothing that would have expose his association with
leftwing radicals and race haters, like a certain minister.
He proved PT Barnum right Funny how a lot of the suckers are still gonna
vote for him. in spite of his record of No Hope and No Effective Change.