Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Generics,refleksija i sl.

[es] :: .NET :: Generics,refleksija i sl.

[ Pregleda: 447 | Odgovora: 4 ]

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

bags
Đorđe Torbica
sIT Solutions, Erste Sparkasse
Vienna, Doboj, Zemun

Član broj: 10072
Poruke: 612
*.12.15.tuwien.teleweb.at.

Jabber: bags84@elitesecurity.org
ICQ: 224667189
Sajt: sledecastanica.wordpress...


Profil

icon Generics,refleksija i sl.04.05.2008. u 23:16

Imam metodu public T GetInstance<T>().Preko refleksije za vreme izvodjenja pokupim Type (instanceType) koji trebam instancirati i pozovem metodu ovako:

Code:

getInstance<instanceType>();


i naravno javlja gresku kako nema tog namespace-a.

Gdje gresim?



while ( ! ( succeed = try() ) );
04.05.2008. u 23:16 

mmix
Miljan Mitrovic
Software Architect
Pancevo, Srbija

Moderator
Član broj: 17944
Poruke: 1612
195.252.78.*



Profil

icon Re: Generics,refleksija i sl.05.05.2008. u 08:58
Generics radi nad tipovima, ne nad instancama. AKo dobro pretpostavljam ti si radio nesto ovako:

Code:

Type instanceType;
isntanceType = reflectionUzmiType(nesto, nesto, ..);
getInstance<instanceType>();

A to ne moze, instanca tipa Type nije tip, ona je deskriptivni objekat koji opisuje tip i sam po sebi je najobicniji objekat i ne moze da se iskoristi kao Generics parametar. Malo deluje zbunjujuce ali iz istog razloga ne mozes da napravis sledeci code

Dictionary<"pera", 123>

vec moras

Dictionary<string, int>


▪ The word 'politics' is derived from the word 'poly', meaning 'many', and the word 'ticks', meaning 'blood sucking parasites' - Larry Hardiman
▪ If the good guy gets the girl, it's rated PG; if the bad guy gets the girl, it's rated R; and if everybody gets the girl, it's rated X
▪ Illegal aliens have always been a problem in the United States. Ask any Native American
05.05.2008. u 08:58 

bags
Đorđe Torbica
sIT Solutions, Erste Sparkasse
Vienna, Doboj, Zemun

Član broj: 10072
Poruke: 612
*.12.15.tuwien.teleweb.at.

Jabber: bags84@elitesecurity.org
ICQ: 224667189
Sajt: sledecastanica.wordpress...


Profil

icon Re: Generics,refleksija i sl.05.05.2008. u 09:32
"Osjetio" sam da je to u pitanju.

Ima li mozda neko zaobilazno resenje osim Refelction.Emit-a ?
while ( ! ( succeed = try() ) );
05.05.2008. u 09:32 

_prompt

Član broj: 64725
Poruke: 29
*.adsl-a-1.sezampro.yu.



Profil

icon Re: Generics,refleksija i sl.05.05.2008. u 09:56
Pogledaj Activator.CreateInstance metod ili kreiraj Assembly objekat
pa iz njega pozovi CreateInstance.
05.05.2008. u 09:56 

mmix
Miljan Mitrovic
Software Architect
Pancevo, Srbija

Moderator
Član broj: 17944
Poruke: 1612
195.252.78.*



Profil

icon Re: Generics,refleksija i sl.05.05.2008. u 10:04
Ne treba ti aktivator u ovoj situaciji, sve sto ti treba vec imas.

Emit se koristi za generisanje novih tipova, ne za instanciranje postojecih. Ako hoces da napravis instancu tipa koji je opisan sa instanceType koristis sledeci kod:

Code:

ConstructorInfo constr = instanceType.GetConstructor(Type.EmptyTypes);
object instance = constr.Invoke(null);


ovaj kod instancira objekat koristeci default konstruktor (onaj bez parametara), ako hoces da pozoves konstruktor sa odredjenim potpisom, pogledaj help za GetConstructor() i Invoke() metode.


▪ The word 'politics' is derived from the word 'poly', meaning 'many', and the word 'ticks', meaning 'blood sucking parasites' - Larry Hardiman
▪ If the good guy gets the girl, it's rated PG; if the bad guy gets the girl, it's rated R; and if everybody gets the girl, it's rated X
▪ Illegal aliens have always been a problem in the United States. Ask any Native American
05.05.2008. u 10:04 

[es] :: .NET :: Generics,refleksija i sl.

[ Pregleda: 447 | Odgovora: 4 ]

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.