Tuesday, 15 March 2011

c# 4.0 - Can I make a dynamic interface in C#? -



c# 4.0 - Can I make a dynamic interface in C#? -

i have created custom class (dynamicitem) inherits dynamicobject class , works great dynamic keyword. dynamicitem implements , interface because know of properties occur, test looks this:

[test] public void interfacetest() { //assign item item = _db.getitem(targetpath); dynamic d = new dynamicitem(item); idynamicitem = d idynamicitem; //act string result = d.title; string path = i.path; //assert assert.areequal("awesome", result); assert.areequal(item.path, path); }

the "title" property not defined on interface , dynamically invoked. "path" property defined on interface.

this test passes , works expect. thing annoys me have cast dynamic interface. able utilize interface:

[test] public void interfacetest() { //assign item item = _db.getitem(targetpath); idynamicitem d = new dynamicitem(item); //act string result = d.title; string path = d.path; //assert assert.areequal("awesome", result); assert.areequal(item.path, path); }

however if compiler complains because can't find "title" property on interface. there way of flagging interface beingness dynamic @ same time beingness interface compiler?

no, cannot create dynamic interface. interface pre-defined set of methods , properties components using / implementing interface know ahead of run-time. if component implements interface, gives guarantee methods / properties of interface available users of component. makes possible replace 1 component one, long new 1 implements same interface.

a dynamic object, on other hand, has nil pre-defined - layout generated @ run-time, based on conditions may alter on time. if function returns dynamic object, may homecoming different dynamic objects (with different properties) if phone call multiple times. when caller calls such function, there's no guarantee layout of returned object. opposite of interfaces.

as @stevelillis commented, it's rare need dynamic object. in situations, can utilize dictionary<string, object> (or similar along lines) , more type-safe approach that's less confusing follow else. in many years of programming, needed utilize dynamic keyword 2 or 3 times - in other situations, chosen info construction sufficient - statically typed , type-safe (well, extent).

here's 1 possible illustration of doing you're trying do:

public interface imydynamicitem { string someitemname { get; set; } object this[int nfieldindex] { get; set; } object this[string sfieldname] { get; set; } ilist<string> fieldnames { get; } } public class mydynamicitem : imydynamicitem { private dictionary<string, object> m_ofields = new dictionary<string, object> (); public string someitemname { get; set; } public object this[int nfieldindex] { { string sfieldname = fieldnames[nfieldindex]; homecoming ( m_ofields[sfieldname] ); } set { string sfieldname = fieldnames[nfieldindex]; m_ofields[sfieldname] = value; } } public object this[string sfieldname] { { homecoming ( m_ofields[sfieldname] ); } set { m_ofields[sfieldname] = value; } } public ilist<string> fieldnames { { homecoming ( new list<string> ( m_ofields.keys ) ); } } }

i'm not sure if best approach give you're trying accomplish it's less ideal - true interface members can accessed through dot notation (that is, oitem.someitemname) dynamic members can accessed through indexer notation (oitem[2] or oitem["somefield"]).

it'd possible add together in interface members internal dictionary members can looked through indexer notation - sense approach dirty. in such scenario, i'd separate out dynamic fields true dictionary , not seek maintain illusion fields somehow part of interface - may more typing code lot cleaner.

c# c#-4.0

No comments:

Post a Comment