Skip to content

Sumários

Miguel Gamboa edited this page Apr 26, 2021 · 43 revisions

Aulas:

Reference: Essential .NET, Volume 1: The Common Language Runtime



  • Virtual Execution Environment
  • Informally virtual machine or runtime
  • C# is NOT a goal
  • Historic evolution since Java to .Net nowadays
  • Examples of languages targeting JVM: Java, kotlin, Scala, Clojure
  • Examples of languages targeting .Net: C#, F#, VB,
  • C# essentials is equal to Java, E.g. class A { static void foo() { }}

  • Component - Reusable software unit, with:
    • IR - code in intermediate representation (e.g. bytecodes, IL, other)
    • Metadata - auto description
    • Ready to use => does not require static compilation.
    • API => conforms to a public interface.
    • Indivisible => 1 module (file)
  • Software development by Components:
    • Reduce Complexity;
    • Promote Reuse.
  • Developer roles:
    • Provider - provides a component with a well-known API (e.g. Point)
    • Client - uses the component from a provider to build an Application, or another component (e.g. App).
  • Unmanaged <versus> Managed
  • Static <versus> Dynamic link

  • Building unmanaged components with static link:
    • NOT a unit, but two parts instead: header + obj
    • Need of a header file that describes the component content
    • Different builds for different architectures
    • Structural modifications (new header) => compilation + link
    • Behavioral modifications => link
  • Demo with an unmanaged App using a Point.
  • VS tools for C unmanaged:
    • cl /c -- compile and produces an obj
    • link comp1.obj comp2.lib etc -- static link
  • Demo with a managed App using a Point.
    • Jitter (just-in-time compiler) - compiles IR to native code (e.g. x86, amd64, ppc) at runtime
    • Dynamic link - Point.class on App compilation + link at runtime.
    • Lazy Load - Point.class is loaded only when needed

  • Solving TPC01
  • Comparing Java components with Dotnet componentes: .class versus .dll
    • .class with a single type
    • .dll with zero or more types
  • Using Java disassemble tool: javap -c ...
  • Using Dotnet disassemble tool: ildasm
  • Highlight metadata and intermediate representation (bytecode or IL)
  • Implicit definitions:
    • default constructor: <init> (Java) and .ctor (dotnet)
    • call base constructir: invokespecial Object."<init>" and call Object::.ctor
  • dependences
    • Java classpath
    • dotnet .csproj and reference to library

  • Dotnet Type System -- CTS (Common Type System) ECMA 335
  • Reflection -- System.Reflection in dotnet and java.lang.reflect in Java.
  • Reflection object oriented API for metadata
  • Dotnet Reflection API (System.Reflection Namespace): Type, FieldInfo, MethodInfo, ConstructorInfo, PropertyInfo, EventInfo and abstract class MemberInfo, which is the base type for aforementioned types.
  • Java Reflection API (java.lang.reflect): Class, Member, Method e Field.

  • Type is representing a type in Dotnet, whereas Class is representing a type in Java.
  • Assembly is representing a component in Dotnet (.dll)
  • Assembly::LoadFrom(String path) -- Static method returns an Assembly instance for the component in given path
  • Assembly::GetTypes() -- returns an array of Type instances for all types in that Assembly instance (i.e. component).

  • Homework:
    • Load(name) versus LoadFrom(path)
    • fully qualified assembly name e.g. "SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3"
    • GetTypes(), GetMethods(), MemberInfo.Name
  • Dotnet solution and dependencies:
dotnet new sln
dotnet new classlib -o Logger
dotnet sln add Logger\Logger.csproj
dotnet new console -o App
dotnet sln add App\App.csproj
cd App
dotnet add reference ..\Logger\Logger.csproj
  • Distinguishing the role of App ---> Logger.
  • Version 1: LogFields and LogMethods:
    • FieldInfo::GetValue(object target);
    • MethodInfo::Invoke(object, object[])
    • MethodInfo::GetParameters() and ParameterInfo
  • Version 2: LogMembers

  • Java runtime type information – Class;
  • Dotnet runtime type information – Type;
  • GetType() returns the same object (same identity) for objects of the same class.
  • VM maintains a single info Type instance for each loaded type.
  • getClass() <=> GetType()
  • p is Point <=> p instanceof Point
  • C# operator as returns null on unsuccess;
  • Cast throws exception on unsuccess;

  • Unit tests franeworks, E.g. JUnit, TestNG, NUnit, XUnit, and others
  • dotnet new xunit
  • How Unit tests franeworks distinguish test methods from others?
    • By name prefix;
    • By annotation, e.g. @Test, [Test], Fact
  • AAA idiom: Arrange, Act and Assert
  • How to test a void method like Info()?
  • Replace dependency Log ----> Console with a new IPrinter.
  • Log uses by default an instance of ConsolePrinter, whereas unit tests use a BufferPrinter to collect the output.
  • Augment Logger API to include a ToLog annotation that let clients select which members should be logged.

  • .net typeof(...) <=> Java .class
  • GetType() vs typeof -- run time <versus> compile time
  • Custom attributes usage:
    • let developers annotate code, e.g. [ToLog] string name; ...
    • stored in metadata
  • Defining new custom attribute: a class that extends Attribute
  • Inspecting custom attributes at runtime through the Reflection API:
    • Attribute::IsDefined(MemberInfo, Type) --> boolean
    • Attribute::GetCustomAttribute(MemberInfo, Type) --> object
    • MemberInfo::GetCustomAttributes(MemberInfo, Type) ---> object[]
  • Custom attributes with parameters. E.g:
    • class ToLogAttribute : Attribute { public ToLogAttribute(String label) { ...
    • Usage: [ToLog("Blue and White")] string name; ...
  • AttributeUsageAttribute => constraints on target and AllowMultiple, E.g:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Method, AllowMultiple=true)]
public class ToLogAttribute : Attribute {
    public ToLogAttribute(String label)
    {
      ...
  • Logger supporting Arrays
  • Arrays are compatible with IEnumerable
  • Compatibility check alternatives:
    1. typeof(IEnumerable).IsAssignableFrom(o.GetType())
    2. o is IEnumerable ? Inspect((IEnumerable) o) : ...
    3. IEnumerable seq = o as IEnumerable; ... seq != null ? Inspect(seq) : ...
  • Details about IL operators isinst and castclass
  • Reflection API IsSubclassOf and IsAssignableFrom
  • Overhead of ShouldLog with 4 verifications and an isinst conversion:
if(!Attribute.IsDefined(m,typeof(ToLogAttribute))) return false;
if(m.MemberType == MemberTypes.Field) return true;
return m.MemberType == MemberTypes.Method  && (m as MethodInfo).GetParameters().Length == 0;
  • Refactor Logger to minimize the use of Reflection checks:
    • Keep Dictionary<Type, List<MemberInfo>>();

  • Refactor Logger to avoid if/else validation on LogMembers() execution.
  • Drop Dictionary<Type, IEnumerable<MemberInfo>>()
  • Use Dictionary<Type, IEnumerable<IGetter>>()
  • interface IGetter { string GetName(); object GetValue(object target);}
  • Distinct implementations: GetterField, GetterMethod, ...
  • Must use the same approach on FireMapper Workout with an interface specifying the way of dealing with properties

  • Stack based execution model.
  • Arguments and local variables tables.
  • Operators st... and ld....
  • call operator.
  • Compile with optimizations: dotnet build -c release

Clone this wiki locally