我有这样的课程:
class MyDate
{
int year, month, day;
}
class Lad
{
string firstName;
string lastName;
MyDate dateOfBirth;
}
Run Code Online (Sandbox Code Playgroud)
我想将一个Lad
对象变成一个像这样的JSON字符串:
{
"firstName":"Markoff",
"lastName":"Chaney",
"dateOfBirth":
{
"year":"1901",
"month":"4",
"day":"30"
}
}
Run Code Online (Sandbox Code Playgroud)
(没有格式化).我找到了这个链接,但它使用的是一个不在.NET 4中的命名空间.我也听说过JSON.NET,但是他们的网站目前似乎已经关闭了,我并不热衷于使用外部DLL文件.除了手动创建JSON字符串编写器之外还有其他选项吗?
我有一个文本框,我想以人类可读的方式在其中显示一个C#对象,仅用于调试原因.如果可能,我不想使用外部库.我该怎么做呢?
可能的重复:
接口与基类
在设计C#类库时,何时应该在接口上选择继承?
所以我正在用C#编写我的第一个真正的程序.该计划将从四个不同的网站获取数据.我的计划是让一个父类看起来像这样:
class Scraper
{
string scrapeDate(url);
string scrapeTime(url);
//&c.
}
Run Code Online (Sandbox Code Playgroud)
然后我会有四个继承它的类.
另一种选择是创建Scraper
一个接口,并有四个实现它的类.
这些方法有什么区别?
我想用它来调用网页上的一些JS脚本.我有这个:
static void Stuff()
{
WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.iana.org/domains/example/");
HtmlDocument doc = browser.Document;
//doc.InvokeScript("someScript");
Console.WriteLine(doc.ToString());
}
static void Main(string[] args)
{
Console.WriteLine("hi");
var t = new Thread(Stuff);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
Run Code Online (Sandbox Code Playgroud)
问题1:当我尝试获取时,我得到"对象引用未设置"异常doc.ToString()
.为什么?
问题2:如何从HTML文档中将一些数据导入主程序?WebBrowser
需要一个单独的线程,这需要一个不能返回任何值的静态方法.如何返回,比如说,doc
在Main()
这样我就可以用它做什么?
我正在使用Managed WiFi API和示例代码:
string profileName = "Cheesecake"; // this is also the SSID
string mac = "52544131303235572D454137443638";
string key = "hello";
string profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", profileName, mac, key);
wlanIface.SetProfile( Wlan.WlanProfileFlags.AllUser, profileXml, true );
wlanIface.Connect( Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName );
Run Code Online (Sandbox Code Playgroud)
我更新了代码以使用我的SSID和密钥,但我不知道如何获取MAC地址.
我正在学习winforms,我为自己设定了一个简单的目标,即制作一个从空到满的进度条.这是我的畸形尝试:
public partial class Form1 : Form
{
static BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync();
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
for(int i=0; i<100; ++i)
{
progressBar1.PerformStep();
Thread.Sleep(10);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我很确定这Thread.Sleep()
是应该受到谴责的.我怎么在这里避免它?
我正在开发一个使用托管WiFi的应用程序.通常我只是将.dll添加到项目中,但是这个库是一个常规的VS项目.我如何在我的程序中使用它?我尝试将Interop.sc和WlanApi.cs添加到我的项目中,但无法识别WiFi类.
我有这个接口和一个实现它的类:
interface Scraper
{
string DateToUrl(DateTime date);
}
class ScraperA: Scraper
{
string Scraper.DateToUrl(DateTime date)
{
return "some string based on date";
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试一下.我尝试将此方法添加到ScraperA
:
public void JustATest()
{
DateTime date = new DateTime(2011, 5, 31);
string url = DateToUrl(date);
Console.WriteLine(url);
}
Run Code Online (Sandbox Code Playgroud)
我把它放在类定义中,但编译器抱怨它无法找到DateToUrl
.为什么?