Aklımda Kalası Kelimeler

* давайте работать вместе
* Zarf ve Mazruf, Zerafet(xHoyratlık) ile aynı kökten(za-ra-fe) gelir
* Bedesten
* Suç subuta ermiştir - Suç sabit olmuştur
.NET etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
.NET etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

10 Şubat 2012 Cuma

Reflection

Ref: Aspect-Oriented Programming with C# and .NET [Wolfgang Schult and Andreas Polze | Hasso-Plattner-Institute at University Potsdam {wolfgang.schult|andreas.polze}@hpi.uni-potsdam.de]

Benim Notlarım

.NET defines a type system and introduces notions such as component, object, and interface, which are building blocks for distributed multi-language component-based applications.

As many other component frameworks, .NET mainly focuses on functional interfaces of components. Nonfunctional aspects of components, such as resource usage (CPU, memory), timing behavior, fault-tolerance, or security requirements are currently not expressed in .NET’s component interfaces.

There exist a number of distributed component frameworks, notably the
  1. Common Object Request Broker Architecture (CORBA)
  2. Microsoft’s Distributed Component Object Model (DCOM/COM+)
  3. SUN’s JavaBean Model
  4. .NET framework

3. Overview over the .NET Architecture

Almost a year ago, Microsoft has introduced the .NET architecture as a new component-based programming environment, which allows for easy integration of classical distributed programming techniques with Web computing. At the center of the .NET framework is an object model, called the Virtual Object System (VOS), and at center of the object model is a type system. The object model relies on basic concepts found in many objectoriented languages, such as class, inheritance, dynamic binding, class-based typing and so on. The object model is not, however, identical to the model of any of these languages. Rather, it’s an attempt to define a suitable base that bridges all these languages and others. The type system of .NET gives objects of predefined basic types, such as integers and characters, a clear place in the type system–and it provides a clean way to convert between reference and value types through "boxing" and "unboxing" operations. The result is a more coherent and regular type system than we have seen in the dominant languages so far.

Most importantly, this model is designed to be language-independent. The C# programming language directly reflects the .NET object model. NET’s focus is rather on the programming model than on any specific language. The .NET framework itself is languageindependent and attempts to provide a reasonable target to which all current languages can map. The framework enables compilers for multiple languages (namely C#, C++, VB) to share a common back end. Multilanguage component mechanisms have existed before, notably CORBA and COM. But they contain a major hurdle – one has to write an interface description in the appropriate interface definition language (IDL) for every component that you make available to the world. There is no IDL with .NET: You just use classes from other languages as if they were from your own. What this means for both component developers and component users is a dramatic simplification of the requirements put on any single development environment. You don’t need libraries addressing every application area. You provide components in your domains of expertise, where you can really bring added value. Where good libraries already exist, you benefit from them at no extra cost.

4. Metadata and Reflection in .NET

Reflection is a language mechanism, which allows access to type information during runtime. Reflection has been implemented for various object-oriented programming languages, among them Java, C#, and C++. C++ is somewhat special as it implements reflection rather as an add-on (RTTI - runtime type information) than as an inherent language feature. With .NET, reflection is not only restricted to a single language, but basically anything declared as code (any .NET assembly) can be inspected using reflection techniques. There are two variants of accessing runtime type information in .NET: the reflection classes in the common language runtime library and the unmanaged metadata interfaces.

4.1. Reflection via Runtime Library

The runtime library´s reflection classes are defined in the namespace of System.Reflection. They build on the fact that every type (class) is derived from Object. There is a public method named GetType, which has as return value an object of the type Type. This type is defined in the namespace System. Every type-instance represents one of three possible definitions:
  1. · a class definition
  2. · an interface definition
  3. · a value-class (usually a structure)
Via reflection, one may ask about almost every type attribute, including the type’s access-modifier, whether it is a nested type and about the type’s properties.
Metadata information is structured in a hierarchical fashion. At the highest level stands the class System.Reflection.Assembly. An assembly object corresponds to one or more dynamic libraries (DLLs) from which the .NET unit in question is composed. As depicted in Figure 1, class System.Reflection.Module stands on the next lower level of the metadata hierarchy. A module represents a single DLL. This module class accepts inquiries about the types the module contains. Proceeding further down the metadata hierarchy reveals type information for any of the building blocks making up a member of the .NET virtual object system.


· method (System.Reflection.MethodInfo)
· constructor(System.Reflection.ConstructorInfo )
· property (System.Reflection.PropertyInfo)
· field (System.Reflection.FieldInfo)
· event (System.Reflection.EventInfo)

Figure 2 presents an excerpt from a C++ program, which uses reflection to display all methods of a given type (MyCalculator in our case).

4.2. The Unmanaged Metadata Interfaces

The unmanaged metadata interfaces are a collection of COM interfaces that are accessible from “outside” of the .NET environment. You can access them from any
Windows program. The interface definition can be found in the COR.H, which is contained in the platform software development kit (platform SDK). The interface
IMetaDataImport.IMetaDataAssemblyImport is used for accessing metadata on the .NET assembly level. Access to this interface is obtained via a second interface,
called IMetadataDispenser. As the name indicates, this interface “dispenses” all kinds of additional metadata interfaces, which allow read and write access to .NET metadata. Access to the metadata dispenser is obtained via calls to the COM system as depicted in Figure 3 (here as C++ Code):

The IMetaDataImport interface obtained from the OpenScope() call provides access to the .NET assembly specified in the wszFileName Argument. Information
about the structure of classes contained in that particular .NET assembly and their building blocks is now accessible via functions EnumXXX and GetXXXProps. The
first function returns an enumeration of tokens describing the metadata available, the latter one returns information about the metadata’s properties, which correspond to a particular token. In addition to the token there exists a special way of type encoding. The function GetMethodProps for example gives an array of the type PCOR_SIGNATURE as return value. This array contains the signature of the queried element. The same information can be obtained by multiple calls to EnumXXX and GetXXXProps, however, using the signatures is the more direct approach. Signatures contain only pure type information, whereas GetXXXProps methods reveal also formal parameter names.

5. A C# Attribute to express Fault-tolerance Requirements

Within this Section we are presenting a simple calculator in C# as a case study. We use the calculator as basis for a discussion on how functional (C#) and nonfunctional (aspect) code can be combined.

16 Ocak 2011 Pazar

.Net: İstisna fırlatırken dikkat edilecek hususlar 3 (Debug.Assert)

Ne zaman Debug.Assert kullanmalıyım?


Debug.Assert için stackoverflow.com da güzel bir soru ve tonla cevap var.



Bu da beğendiğim bir cevap:
You should use Debug.Assert to test for logical errors in your programs. The complier can only inform you of syntax errors. So you should definetely use Assert statements to test for logical errors. Like say testing a program that sells cars that only BMWs that are blue should get a 15% discount. The complier could tell you nothing about if your program is logically correct in performing this but an assert statement could.

.Net: İstisna fırlatırken dikkat edilecek hususlar 2


Tek bir try ile sardık.
Stack Trace:

at caException.Program.f_Bol(Int32 a, Int32 b) in D:\Projeler\caException\caException\Program.cs:line 23
at caException.Program.Main(String[] args) in D:\Projeler\caException\caException\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()




Stack Trace:

at caException.Program.f_Bol(Int32 a, Int32 b) in D:\Projeler\caException\caException\Program.cs:line 29
at caException.Program.Main(String[] args) in D:\Projeler\caException\caException\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Oysa birincisinde hata satırını cuk diye alıyorduk. Demek ki yapmamız gereken şey, kritik eylem satırlarını kendi içinde bir try catch bloğuna sarmalıyız.

.Net: İstisna fırlatırken dikkat edilecek hususlar 1

Aslında başka bir durumu araştırmak için baktım ama o kadar ekran yakalama boşa gitmesin :)
Basit ama yapılan hatalardan.
İstisna fırlatılırken yeni bir istisna fırlatmamak ve istisnaya neden olan durumu örtmemek gerek.



2 Ocak 2010 Cumartesi

VS ve Java Generic Metot

Visual Studio 2008 de generic parametre alan bir metot şu şekilde:

vs

Çıktısı:

vsOut

Java’da :

java

javaout


Peki birden fazla generic tipte parametre alan metot nasıl olur?

Visual Studio 2008:

vsParam

Java’da:

javaParam

javaParam1

Java da T generic parametreleri derleme zamanında Object tipine çeviriliyor. Buna “erasure” deniyor. Hadi bir kayak:

http://java.sun.com/docs/books/tutorial/java/generics/erasure.html

Ama VS da durum bu değil. Peki ne? Bende bilmiyorum. Bilen olursa yazsın :)

Hadi birazda çıktılarına bakalım.

javaErasure vsErasure

14 Ekim 2007 Pazar

Platform Invoke nedir ? (Platform Invocation Services)

unmanaged olarak yazılmış fonksiyonların managed ortamda çağırılıp çalıştırılabilmesidir. yani .NET dilleri içinde unmanaged olarak yazılmış bir fonksiyonun çalıştırılabilmesi olayıdır. Tam adı Platform Invocation Services'tir fakat Platform Invoke olarak geçer, birçok yerde de P/Invoke denir. .NET'in yetersiz veya yavaş kaldığı yerlerde hızır gibi imdadımıza yetişir.


[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBox(int hWnd, String text, String caption, uint type);


yukarıdaki kod örneği user32.dll içindeki MessageBox fonksiyonunu managed kodumuzda (örnekte C#) kullanabilmemizi sağlar.

Kullanımı:

MessageBox(0, "heey, bilişim kulübü, ben user32.dll içinden geliyorum",
"Yabancı ama tanıdık bi yüz", 0);


Aşağıdaki kod da VB.NET örneği

Declare Auto Function MessageBox Lib "user32.dll" (ByVal hWnd As Integer, ByVal txt As String, _
ByVal caption As String, ByVal Typ As Integer) As Integer



Kaynak: bilisim-kulubu.com

Managed ve UnManaged KOD ne demektir?

Managed kod demek hafıza yönetimi(memeory management) yonetilen demek oluyor.Oluşturduğun değişkenlerin yok edilmesi işini kullandığın sistem(FrameWork, Programlama dili...) yapıyor demektir.

Örneğin:
SqlCommand cmd = new SqlCommand();
..
...
....
cmd.Dispose(); //işin bitti uçur bellekten..


Bunu kendin yaptın.Ama sen eğer x kadar süre kullanmazsan Çöp Toplayıcı(Garbage Collector) bunu senin yerine yapacaktı. Bunu senin yerine yapan bir sistemde yazılan kod MANAGED CODE(yönetilen kod) demektir.


UNMANAGED KOD(Yönetilmeyen Kod) ise tam tersidir.Yani bellek yonetimi senin tarafından yapılan, başkası tarafindan yonetilmeyen demek oluyor. Yukarıdaki örneğe göre, cmd.Dispose() işini daima senin yapman gereken kodlama.

Örneklemek gerekirse VC++ 6 programı unmanaged koddur. Çunku onda memory yonetimi (Allocation ve Deallocation lar objeleri delete le silme falan) kodcu tarafından yapılır.

Ancak .NET'te ve Java'daGC (Garbage collector - Çöp toplayıcı) vardır.

Garbage collector Nedir? Nedir bu GC ?
GC .NET te hafıza işlerini yoneten mekanizmadır ve Java bu işin öncüsüdür. Yani programının bellekt yonetimi onun tarafından yapılır. Bir obje yarattığında (new le) onun için bellekte bir alan açmak daha sonra bu obje ile işin bittiğinde ona artık herahangi bir referans kalmadığında onun Dispose edilmesi ve Finalize edilmesi gibi işlemleri yapar. Memoryde alan sıkıntısı olduğunda çalışıp yok edebileceği objeleri kaldırır. Bu nedenle her nesne için önce alloc sonrada işin bittiğinde delete demek zorunda kalmazsın.