January 30th, 2009 — 7:32am
Reflection ile out parametreli bir methodu çalıştırmak için ParameterModifier dizisi kullanmamız gerekiyor. (1 method için 1 uzunluklu bir dizi oluşturup. 2 parametre için de dizinin elemanına 2 elemanlı bir diziymiş gibi davranıp true değerini verebiliriz. bi türlü dile getiremedim :P)
true ref ve out için veriliyor, false da normal parametreler için.
Parametrelerin türünü belirtirken Type.GetType metoduna string olarak uzun adını yazıp sonuna da bir & işareti koyuyoruz. Bu işarette ref veya out olduğunu belirtmenizi sağlıyor.
string reflecting = "";
string something = "";
Assembly asm = Assembly.LoadFile(
string.Format(@"{0}\Deneme.dll",Environment.CurrentDirectory));
Object pro = asm.CreateInstance("Deneme.Test");
ParameterModifier[] arrPmods = new ParameterModifier[1];
arrPmods[0] = new ParameterModifier(2);
arrPmods[0][0] = true; // out için true
arrPmods[0][1] = true;
System.Type[] arrTypes = new System.Type[3];
//ref ve out parametreleri için & işareti anahtar mevzu
arrTypes.SetValue(Type.GetType("System.String&"), 0);
arrTypes.SetValue(Type.GetType("System.String&"), 1);
object[] prms = new object[] { reflecting, something };
pro.GetType().GetMethod("Topla", arrTypes, arrPmods).Invoke(pro, prms);
Console.WriteLine(string.Format("değer 1:{0}", prms[0]));
Console.WriteLine(string.Format("değer 2:{0}", prms[1]));
Console.ReadLine();
Comment » | Csharp - C#
January 30th, 2009 — 4:08am
HttpWebRequest nesnesiyle sayfaların html kaynaklarını çekmek bazı sayfalarda sorun yaşatabiliyor. Mesela wikipedia‘nın kaynağını çekerken yaşadığım Accept özelliği hatası gibi. Eğer bir değer atamazsak bazı sayfalar hataya düşüyor. Fakat içine “serdar :)” bile yazsak sayfa geliyor.
css gibi özel tipleri çekerken de sorun yaşamamak için “text/*” gibi genel bir Accept tanımlması yapılmalı…
Uri uri = new Uri("http://en.wikipedia.org/wiki/Special:Export/Train");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
//text/* yerine text/html yazarsak css çekerken 406 hatası alırız
//The remote server returned an error: (406) Not Acceptable.
//accept kullanmadığımız durumlarda bazı sayfalar 403 hatası verebilir.
//The remote server returned an error: (403) Forbidden.
httpWebRequest.Accept = "text/*";
WebResponse webResponse = httpWebRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string html = streamReader.ReadToEnd();
streamReader.Close();
stream.Close();
webResponse.Close();
Console.Write(html);
Console.Read();
HttpRequest
wikipedia
MIME types
4 comments » | Csharp - C#
January 30th, 2009 — 3:32am
by using this static method we can shutdown our web application. Maybe it is not so useful, but we should know…
//Terminates the current application, it restarts next time it is requested.
System.Web.HttpRuntime.UnloadAppDomain();
msdn
Comment » | Asp.Net
January 26th, 2009 — 4:55am
When we need javascript actions from asp.net we need to use RegisterClientScriptBlock method.
it is a better practice to make a js actions class and put your common js methods in it.
this little method below alerts from any page when you call the JsAlert Method.
JsActions.JsAlert("Hi Serdar ;)");
public static class JsActions
{
public static void JsAlert(string alertText)
{
Page p = HttpContext.Current.Handler as Page;
p.ClientScript.RegisterClientScriptBlock(p.GetType(),
"myAlert",
string.Format("<script>alert('{0}');</script>", alertText));
}
}
dotnetslackers
Comment » | Asp.Net
January 21st, 2009 — 7:17am
Reflection ile bir dll in içindeki method ve property’lere erişmeyi gösteren küçük bir örnek…
Önce Activator.CreateInstance ile nesnenin örneğini oluşturuyoruz, sonra GetProperty ile özelliği alıp SetValue ile de değeri veriyoruz…
GetMethod().Invoke() ile de class’ın içinden bir metodu çalıştırabiliriz…
BaseReport rpt = Activator.CreateInstance(Type.GetType("ReportClass")) as BaseReport;
rpt.GetType().GetProperty("Paremetre").SetValue(rpt, "Ankara", null);
rpt.GetType().GetMethod("Work").Invoke(rpt, new object[] { "Parametre" });
msdn
code guru
csharp nedir?
Comment » | Csharp - C#
January 21st, 2009 — 6:30am
Aşağıdaki satırları biz yazsak görüşmeye bile çağırmazlar… Gerçekten böyle hissedebiliriz. (Steve jobs ile kıyaslandığında great experiences hariç :) ) Bunu yapabilecek güçte de olabiliriz. Ama buna sadece kendimiz firmamızı kurduğumuzda patronumuzu inandırabiliriz :) Zaten insan her zaman kendine inanmalı!
I’m looking for a fixer-upper with a solid foundation. Am willing to tear down walls, build bridges, and light fires. I have great experience, lots of energy, a bit of that “vision thing” and I’m not afraid to start from the beginning.
Steve Job’s Resume
Steve abimize acil şifalar da diliyelim…
Comment » | Something