Saturday, December 08, 2007

Reflection is the feature in .Net, which enables us to get some information about object in runtime. That information contains data of the class. Also it can get the names of the methods that are inside the class and constructors of that object.

   To write a C# .Net program which uses reflection, the program should use the namespace System.Reflection. To get type of the object, the typeof operator can be used. There is one more method GetType(). This also can be used for retrieving the type information of a class. The Operator typeof allow us to get class name of our object and GetType() method uses to get data about object?s type. This C# tutorial on reflection explains this feature with a sample class.


public class TestDataType
{

public TestDataType()
{
   counter = 1;
}

public TestDataType(int c)
{
   counter = c;
}

private int counter;

public int Inc()
{
   return counter++;
}
public int Dec()
{
   return counter--;
}



At first we should get type of object that was created.

TestDataType testObject = new TestDataType(15);
Type objectType = testObject.GetType();


   Now objectType has all the required information about class TestDataType. We can check if our class is abstract or if it is a class. The System.Type contains a few properties to retrieve the type of the class: IsAbstract, IsClass. These functions return a Boolean value if the object is abstract or of class type. Also there are some methods that return information about constructors and methods that belong to the current type (class). It can be done in a way as it was done in next example:


Type objectType = testObject.GetType();

ConstructorInfo [] info = objectType.GetConstructors();
MethodInfo [] methods = objectType.GetMethods();

// get all the constructors
Console.WriteLine("Constructors:");
foreach( ConstructorInfo cf in info )
{
   Console.WriteLine(cf);
}

Console.WriteLine();
// get all the methods
Console.WriteLine("Methods:");
foreach( MethodInfo mf in methods )
{
   Console.WriteLine(mf);
}


   Now, the above program returns a list of methods and constructors of TestDataType class.

   Reflection is a very powerful feature that any programming language  would like to provide, because it allows us to get some information about objects in runtime.
It can be used in the applications normally but this is provided for doing some advanced programming.
This might be for runtime code generation (It goes through creating, compilation and execution of source code in runtime).

 | 
Saturday, December 08, 2007 11:53:24 PM (GMT Standart Saati, UTC+00:00)
 Wednesday, October 03, 2007

Bildiğiniz gibi asp.net uygulamalarında textboxlara karakter sınırı koymak için maxlength adlı propertisini kullanabiliriz. Fakat textbox'ın textmode=multiline yapınca her nedense bu property çalışmıyor.(Bug mı acaba?)

 

Bu sorunu ekteki javascript'i kullanarak aşabiliriz.

   

    <script type="text/javascript" language="JavaScript">

    function textCounter(field,cntfield,maxlimit) {

        if (field.value.length > maxlimit)

            field.value = field.value.substring(0, maxlimit);

 

        else

            cntfield.value = maxlimit - field.value.length;

       }

    </script>

 

 

 

       <table>

           <tr>

               <td>

                   <input readonly type="text" name="Sayac" maxlength="3" value="230" style="width: 29px" />

                   Kalan karakter sayısı

               </td>

           </tr>

           <tr>

               <td>

                   <asp:TextBox runat="server" ID="txtKisitli" Width="250px" Height="100px" TextMode="MultiLine"

                       onKeyDown="textCounter(document.aspnetForm.txtKisitli,document.aspnetForm.Sayac,230)"

                       onKeyUp="textCounter(document.aspnetForm.txtKisitli,document.aspnetForm.Sayac,230)">

                   </asp:TextBox>

               </td>

           </tr>

       </table>

 | 
Wednesday, October 03, 2007 5:04:10 PM (GMT Standart Saati, UTC+00:00)
 Tuesday, February 06, 2007

Üyelik sistemlerinde sıklıkla karşılaşılan sorulardan biri de "Beni Hatırla" özelliğidir, makalemde Cookie kullanarak "Beni Hatırla" opsiyonunu login sayfalarına nasıl adapte edebileceğinizi anlatacağım.

Web sitesinin kullanıcıları hatırlaması için bir çok yöntem var. Örneğin Mac adresini kullanıcı ile eşleyerek veritabanına yazmak bunlardan biri. Fakat hem kullanım alanının çok verimli olmayışı hem de kullanılabilirliliğinin azlığı nedeniyle pek kullanılan bir yöntem değildir. Diğer ve etkin bir yöntem ise Cookiler.

Cookiler kullanıcı tarafında  saklanan text tabanlı dosyalardır. Bu dosyaların yönetimi, içeriği ve erişimi tamamen cooki'yi yaratan web sitesine aittir. Cookilerin ömürlerini biz belirleyebildiğimiz gibi, içerik değişiklikleri, silinmesi, expire olması gibi tüm özelliklerini de server tarafından yönetebiliriz. Ayrıca kullanıcı isterse cookieleri kapatabileceğinden yada internet tarayıcısı üzerinde bunları rahatlıka temizleyebileceğinden, tüm sistemi kesinlikle cookiler üzerine kurmamalıyız.  Bu yazıda Cookileri web sitesine giriş yaparken, kullanıcıyı hatırlamak için gerekli olan değeri kullanıcının bilgisayarına (Client Side) kaydederek saklamak için kullanacağız. Cookiler kullanıcı tarafında saklandığı için potansiyel güvenlik açıklarını da beraberinde getirir. Bu nedenle, kullanıcıyı tanımlamada kullanacağımız bilgiler, sistemimizi minimum düzeyde tehlikeye sokacak düzeyde olmalıdır. Bu nedenle cookie de saklanacak veri kullanıcı adı ve şifresi olmamalıdır.

Cookie yönetebilen bir login.aspx nasıl olmalı :

Bu uygulamada cookilere yazıcağımız değeri şifrelemek yerine veritabanında unique indetifier olarak tanımladığımız UserId ile dolduruyoruz. Bu bize cookie okunduğu zaman o kullanıcıyla ilgili herhangibir bilginin dışarı sızmasını engelliyor.


Bu sistem bize projemizin business tarafında kullanıcıadı ve şifre denetleyerek kullanıcları tanıma yönteminin dışında UserId kullanarak kullanıcı tanımlama fonksiyonuna ihtiyacımız olduğunu gösteriyor.

Öncelikle buttonumuzun click eventini yazalım:

 

     Cookie.buss buss = new Cookie.buss(); 
     DataTable user = new DataTable();   
     user = buss.GetUser(TextBox1.Text.Trim(), TextBox2.Text.Trim());
//ekrandan girilen kullanıcı adi ve şifreyi doğrulayan kullanıcı varmı?
         
if (user.Rows.Count>0)
         {
            
if (CheckBox1.Checked) //Beni hatırla checkbox'ı
            {
             Response.Cookies[
"behluluser"].Value = user.Rows[0]["UserId"].ToString(); //Cookiemizi yaratıp guid'imizi içine yazalım.
             Response.Cookies[
"behluluser"].Expires = DateTime.Now.AddYears(30);  //Cookiye 30 yıl yaşama şansı verelim.
            }
          Response.Redirect(
"http:\\www.behlulbehram.com"); //kullanıcı başarıyla girdi istediğimiz sayfaya yönlendirelim.
         }
        else 
            Label1.Text = "kullanıcı bulunamadi";

 

Not: Farklı windows kullanıcıları aynı siteye girselerde farklı cookiler yaratılır. Birbirlerini ezmezler. Örneğin A kişisi sistemimize girip beni hatırla derse
A'nın "behluluser" cookie'si yaratılır. Aynı bilgisayaradan B kişisi accountunu açip sisteme giriş yapıp beni hatırlayı seçerse B'nin "behluluser" cookiesi yaratılır.

Şimdide login.aspx'in page loadunda  nasıl bir işlem yapmalıyız onu görelim;

if (!IsPostBack)
{
   if (Request.Cookies["behluluser"] != null)//Kullanıcı Tarafında daha önceden yarattığım Cookie Varmı? 
   

      Cookie.
buss buss = new Cookie.buss();
      
DataTable user = new DataTable();

      
string myguid= Request.Cookies["behluluser"].Value; 
      user = buss.GetUserById(myguid);
         
if (user.Rows.Count > 0)//cookiedeki user gerçekten database de de var
            
{
               Response.Redirect(http://www.behlulbehram.com);
            }
        
else//cookie deki bilgi yanlış  biri oynamış ... silelim.
            
{
              Response.Cookies[
"behluluser"].Expires = DateTime.Now.AddYears(-30);
            }
   }
}

not: Cookileri silmek yerine expire etmek daha verimli bir yöntemdir.

ve login.aspx'i tamamlamış olduk. Derleyip çalıştırabiliriz.

Umarım cookileri nasıl kullanıcağımızla ilgili azda olsa fikir sahibi olmuşsunuzdur.

Sorularınız için

behlul.behram@bilgeadam.com
behlulbehram@gmail.com

Kolay gelsin

Behlul.

 | 
Tuesday, February 06, 2007 7:20:21 PM (GMT Standart Saati, UTC+00:00)
 Monday, December 25, 2006

Globally Unique Identifier.

Ne demek bu Guid ?

Tekil 128 bit lik bir değer diyebiliriz. Bu tekillik oyle boyle bir tekillik diğil ama bir rivayete gore bir pc de uretilen guid değerini bir daha üretmek mümkün değil. Genelde büyük databaselerde primary key olarak kullanılıyor. Yada heran kullandığımız browserlerın açtığımız sayfaların sessionlarını guid kullanarak tutması iyi bir örnek olabilir.

Kullanımı basitce şöyle :=

using System;

namespace Behlul
{
  class MainClass
  {
    static void Main(string[] args)
    {
      System.Guid guid=System.Guid.NewGuid();
      Console.WriteLine(guid.ToString());
      Console.ReadLine();
    }
   }
 
}

işte bir guid := 6e42cdb4-e441-4c8f-8a77-1d2478ffb65a

 

kolay gelsin...

 

 | 
Monday, December 25, 2006 9:49:41 PM (GMT Standart Saati, UTC+00:00)
 Friday, November 10, 2006

Microsoft has released its new operating system, Windows Vista, to hardware manufacturers, marking the end of the development phase and the beginning of the distribution phase.
Everything's not perfect, but Microsoft expects to have all the glitches under control by the company's self-imposed January 2007 product release date.  :)

Bekleyenlerin gözü aydın. Release'i bekleyenler açıklanan tarih : Ocak 2007

 

 | 
Friday, November 10, 2006 9:03:29 PM (GMT Standart Saati, UTC+00:00)
 Wednesday, November 08, 2006

Sonunda kullanıma hazır.

http://msdn.microsoft.com/windowsvista/support/relnotes/netfx3/default.aspx

Buna ek olarak

Windows Presentation Foundation (WPF, önceden Avalon olarak adlandırılıyordu) 
http://en.wikipedia.org/wiki/Windows_Presentation_Foundation

Windows Communication Foundation (WCF, öceden Indigo olarak adlandırılıyordu) 
http://en.wikipedia.org/wiki/Windows_Communication_Foundation

Windows Workflow Foundation (WF)
http://en.wikipedia.org/wiki/Windows_Workflow_Foundation

Windows CardSpace (WCS, öceden InfoCard olarak adlandırılıyordu) 
http://en.wikipedia.org/wiki/Windows_CardSpace

Bu linkleri ziyaret etmenizi tavsiye ederim.

 |  | 
Wednesday, November 08, 2006 12:10:15 AM (GMT Standart Saati, UTC+00:00)
 Monday, October 30, 2006

Google'ın Code araması yapabilen bir engine ara yüzü olduğunu biliyormuydunuz?

http://www.google.com/codesearch


 | 
Monday, October 30, 2006 6:16:57 PM (GMT Standart Saati, UTC+00:00)