Tabbas, ga labarin ku.
C# yana ba da zaษi mai ฦarfi, inganci, da sauฦin amfani don bincika ko a nau'in yana aiwatar da dubawa. Wannan yana da amfani musamman a yanayin yanayi inda nau'in bayanai na iya zama mara tabbas, kuma fahimtar ko yana aiwatar da wani ฦayyadaddun mu'amala zai iya jagorantar dabaru da aiwatar da lambar. A cikin wannan labarin, mun zurfafa cikin cikakkun bayanai na yadda za a iya cimma wannan ta hanyar nuna mafita ga matsalar, bayyana lambar, da kuma nuna ษakunan karatu da ayyuka masu dacewa.
Dubawa Idan Nau'in Yana Aiki Interface - Magani
C # yana ba mu damar tantance idan nau'in da aka bayar yana aiwatar da wani ฦayyadaddun dubawa ta amfani da hanyar IsAssignableDaga hanyar da nau'in nau'in ya samar. Anan ga mafita don kwatanta wannan fasalin.
IMyInterface na jama'a
{
}
MyClass na jama'a: IMyInterface
{
}
MyTest ajin jama'a
{
Babu Jama'a CheckIfImplementsInterface()
{
var myObj = sabon MyClass ();
nau'in var = nau'in (IMyInterface);
var doItImplement = type.IsAssignableFrom(myObj.GetType());
Console.WriteLine (yinImplement); // Sakamakon: Gaskiya
}
}
Fahimtar Code
Mun fara kashewa ta hanyar ma'anar keษance mai suna IMyInterface ba tare da wata hanya ba. Na gaba, mun ayyana ajin MyClass wanda ke aiwatar da wannan keษancewa. Keษancewar ba ta da wata hanya ko kaddarorin, don haka ba ma buฦatar ayyana wani ฦarin abu a cikin MyClass ษin mu.
Sihiri yana faruwa a cikin ajin MyTest. Muna hanzarta MyClass kuma muna samun nau'in abu don IMyInterface. Sannan, muna amfani da hanyar IsAssignableFrom don bincika ko nau'in misalin yana aiwatar da aikin mu. Idan abu ya aiwatar da dubawa, yana fitar da Gaskiya; in ba haka ba, yana fitar da Karya.
Babban aikin anan shine IsAssignableDaga. Wannan hanya, na cikin tsarin System.Type, yana ฦayyade ko za a iya sanya misali na wani nau'i na musamman zuwa misalin nau'in halin yanzu.
Mabuษin Laburaren da Ayyuka
A cikin misalinmu, muna amfani da su System namespace, wanda ke ba da mahimman azuzuwan da azuzuwan tushe masu amfani wajen haษaka aikace-aikace ta amfani da C #. Babban aikin anan shine Nau'in.IsAssignableDaga - aikin da ke bincika idan ana iya sanya misali na wani nau'i zuwa misalin wani nau'in.
- Nau'in: Yana wakiltar nau'in sanarwa: nau'ikan aji, nau'ikan mu'amala, nau'ikan tsararru, nau'ikan ฦima, nau'ikan ฦidayar, nau'in sigogi, nau'in nau'in ma'anar ma'anar, da buษe ko rufe nau'ikan nau'ikan jeri.
- Nau'in.IsAssignableDaga: Yana ฦayyade ko ana iya sanya misali na wani Nau'i daga misalin wani Nau'in.
Wannan fasalin duba ko nau'in yana aiwatar da hanyar sadarwa yana ba da iko mai girma akan kwararar bayanai da dabaru a cikin shirye-shiryenmu na C #. Tare da wannan fahimtar mafita da sabbin ษakunan karatu da ayyuka, zaku iya dacewa da mahimmancin wannan dabarun don tabbatar da cewa bayanan sun yi daidai da takamaiman kwangilolin mu'amala da ku.