public abstract class DynamicObject : IDynamicObject{ public virtual object GetMemberGetMemberBinder info; public virtual object SetMemberSetMemberBinder info, object value; public vi
Trang 3C# 4.0
Dynamic Programming
Trang 4C# 4.0 Language Innovations
Trang 5PythonBinder BinderRubyBinderRuby BinderCOMBinderCOM
JavaScriptBinder
JavaScriptBinder
Trang 6.NET object
object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add",
BindingFlags.InvokeMethod, null,
new object[] { 10, 20 });
int sum = Convert.ToInt32(res);
.NET object
object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add",
BindingFlags.InvokeMethod, null,
new object[] { 10, 20 });
int sum = Convert.ToInt32(res);
Dynamic Language objectScriptObject calc = GetCalculator();
object res = calc.Invoke("Add", 10, 20);int sum = Convert.ToInt32(res);
Dynamic Language object
ScriptObject calc = GetCalculator();
object res = calc.Invoke("Add", 10, 20);
int sum = Convert.ToInt32(res);
Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);
Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
Dynamic method invocation
Dynamic conversion
Dynamic conversion
Trang 7dynamic calc = GetCalculator();int sum = calc.Add(10, 20);
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
Trang 8dynamic i = 3;Math.Abs(i);
dynamic i = 3;Math.Abs(i);
Trang 10Under the cover dynamic d = new DynamicObject();
d.Foo();
dynamic d = new DynamicObject();d.Foo();
Trang 11public abstract class DynamicObject : IDynamicObject
{
public virtual object GetMember(GetMemberBinder info);
public virtual object SetMember(SetMemberBinder info, object value);
public virtual object DeleteMember(DeleteMemberBinder info);
public virtual object UnaryOperation(UnaryOperationBinder info);
public virtual object BinaryOperation(BinaryOperationBinder info, object arg);
public virtual object Convert(ConvertBinder info);
public virtual object Invoke(InvokeBinder info, object[] args);
public virtual object InvokeMember(InvokeMemberBinder info, object[] args);
public virtual object CreateInstance(CreateInstanceBinder info, object[] args);
public virtual object GetIndex(GetIndexBinder info, object[] indices);
public virtual object SetIndex(SetIndexBinder info, object[] indices, object value); public virtual object DeleteIndex(DeleteIndexBinder info, object[] indices);
public MetaObject IDynamicObject.GetMetaObject();
}
public abstract class DynamicObject : IDynamicObject
{
public virtual object GetMember( GetMemberBinder info);
public virtual object SetMember( SetMemberBinder info, object value);
public virtual object DeleteMember( DeleteMemberBinder info);
public virtual object UnaryOperation( UnaryOperationBinder info);
public virtual object BinaryOperation( BinaryOperationBinder info, object arg);
public virtual object Convert( ConvertBinder info);
public virtual object Invoke( InvokeBinder info, object [] args);
public virtual object InvokeMember( InvokeMemberBinder info, object [] args);
public virtual object CreateInstance( CreateInstanceBinder info, object [] args);
public virtual object GetIndex( GetIndexBinder info, object [] indices);
public virtual object SetIndex( SetIndexBinder info, object [] indices, object value); public virtual object DeleteIndex (DeleteIndexBinder info, object [] indices);
public MetaObject IDynamicObject GetMetaObject();
}
Trang 13Improved COM Interoperability
Trang 14object -> dynamic mapping
((Excel.Range)xl.Cells[1,1]).Value2 = “ID”;((Excel.Range)xl.Cells[1,1]).Value2 = “ID”;
We need to cast
xl.Cells[1,1].Value2 = “ID”;
Trang 15Optional and named parameters
xlChart.ChartWizard(cellRange.CurrentRegion, Constants.xl3DBar, PlotBy: Excel.XlRowCol.xlColumns,
written
Arguments evaluated in order
Constants.xl3DBar, Type.Missing, Excel.XlRowCol.xlColumns,
1, 2, false, xlSheet.Name, Type.Missing,
Type.Missing, Type.Missing);
xlChart.ChartWizard(cellRange.CurrentRegion,
Constants.xl3DBar, Type.Missing, Excel.XlRowCol.xlColumns,
1, 2, false, xlSheet.Name, Type.Missing,
Type.Missing, Type.Missing);
Trang 16public StreamReader OpenTextFile(
Encoding encoding = null,
bool detectEncoding = true,
int bufferSize = 1024);
public StreamReader OpenTextFile(
string path,
Encoding encoding = null,
bool detectEncoding = true,
int bufferSize = 1024);
Optional parameters
Optional parameters
OpenTextFile("foo.txt”);
OpenTextFile("foo.txt”);
OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096);
OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096);
Named argument
Named argument
Optional and named parameters
Trang 17Optional “ref” modifier
object fileName = "Test.docx";
object missing = System.Reflection.Missing.Value;
doc.SaveAs(ref fileName,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
object fileName = "Test.docx";
object missing = System.Reflection.Missing.Value;
doc.SaveAs(ref fileName,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
doc.SaveAs("Test.docx");
Trang 18Under the cover
Trang 20void Process(object[] objects) { … }
void Process(object[] objects) { … }
string[] strings = GetStringArray();
void Process(IEnumerable<object> objects) { … }
void Process(IEnumerable<object> objects) { … }
.NET arrays are co-variant
.NET arrays are co-variant
…but not safely
co-variant
…but not safely
co-variant
Until now, C# generics have
been invariant
Until now, C# generics have
been invariant
void Process(IEnumerable<object> objects) {
// IEnumerable<T> is read-only and
// therefore safely co-variant
}
void Process(IEnumerable<object> objects) {
// IEnumerable<T> is read-only and
// therefore safely co-variant
Trang 21public interface IEnumerable<T>
Output positions only
IEnumerable<string> strings = GetStrings();IEnumerable<object> objects = strings;
IEnumerable<string> strings = GetStrings();
IEnumerable<object> objects = strings;
Can be treated as less derived
Can be treated as less derived
public interface IComparer<T>
IComparer<object> objComp = GetComparer();
IComparer<string> strComp = objComp;
in =
Contra-variant Input positions
only
in =
Contra-variant Input positions
only
Can be treated as more derived
Can be treated as more derived
Trang 22Variance in C# 4.0
Trang 25class Base {
public virtual void Foo(int x = 4, int y = 5) {
Console.WriteLine("x:{0}, y:{1}", x, y);
}
}
class Derived : Base {
public override void Foo(int y = 4, int x = 5) {
Console.WriteLine("x:{0}, y:{1}", x, y);
}
}
class Program {
static void Main(string[] args) {
Base b = new Derived();
b.Foo(x: 4, y: 5);
}
}
class Base {
public virtual void Foo(int x = 4, int y = 5) {
Console.WriteLine("x:{0}, y:{1}", x, y);
}
}
class Derived : Base {
public override void Foo(int y = 4, int x = 5) {
Console.WriteLine("x:{0}, y:{1}", x, y);
}
}
class Program {
static void Main(string[] args) {
Base b = new Derived();
Output:
a)x:4, y:5b)x:5, y:4c)x:4, y:4d)x:5, y:5e)None of the above
Output:
a)x:4, y:5b)x:5, y:4c)x:4, y:4d)x:5, y:5e)None of the above
Output:
a)x:4, y:5
b)x:5, y:4
c)x:4, y:4d)x:5, y:5e)None of the above