對不起我的愚蠢,但我正在尋找一種方法來構建一個對象從一個類連接到一個服務,還包含另一個類與服務的方法。C#參考父母
我面臨的問題是試圖找出一種方法,只需要連接到服務一次。我拒絕使用全局變量和類似的東西。
我很難理解C#中的對象概念,因爲我的編程背景主要來自JavaScript。
任何幫助最受讚賞。
namespace Tests.Classes
{
public class L
{
dynamic uri;
dynamic service;
dynamic credentials;
dynamic proxy;
public L L()
{
this.uri = new Uri("bladie bladie blah");
this.credentials = new ClientCredentials();
this.credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
this.proxy = new OrganizationServiceProxy(this.uri, null, this.credentials, null);
this.service = (IOrganizationService)this.proxy;
return this;
}
public class OL
{
public OL OL(dynamic a)
{
this.service = parent.service; // <- doesn't compile
return this;
}
}
}
}
要清楚它是如何叫:
var l = new L();
l.OL("haha");
也許我的代碼是不太清楚。這將使分類狂熱分子保持在海灣:P。
namespace Tests.Classes
{
public class L
{
Uri uri;
IOrganizationService service;
ClientCredentials credentials;
OrganizationServiceProxy proxy;
public L L()
{
this.uri = new Uri("hBladie Bladie Blah");
this.credentials = new ClientCredentials();
this.credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
this.proxy = new OrganizationServiceProxy(this.uri, null, this.credentials, null);
this.service = (IOrganizationService)this.proxy;
return this;
}
public class OL
{
Entity entity = new Entity();
IOrganizationService service = null;
public OL OL(dynamic a)
{
if (a is Entity)
{
this.entity = a;
}
if (a is string)
{
this.entity = new Entity(a);
}
return this;
}
public OL attr(dynamic key, dynamic value)
{
this.entity[key] = value;
return this;
}
public Boolean save()
{
this.parent.service.create(this.entity); // parent does not exist
}
}
}
}
我討厭凌亂的編程,我喜歡jQuery風格。
下面的代碼如何,必須使用:
new L().OL("haha").attr("Hello", "world").save();
或
var l = new L();
l.OL("HAHA").attr("foo", "bar").save();
l.OL("pff").attr("boppa", "deebop").save();
首先,用'var'替換'dynamic'。你不需要動態的。 – Jason 2012-03-16 09:07:51
目前還不清楚你想要什麼'parent',或者爲什麼你所有的變量都是使用'dynamic'聲明的... – 2012-03-16 09:08:43
不回答我的問題,但沒問題 – Chris 2012-03-16 09:08:58