The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.

  今天装了64位win7,编译成功了,放在IIS里运行出错。The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine. 在此记录下来:

The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine. s

在网上google了一下,错误原因是我的机子是64编译的,需要改为32位,在vs中设置将编译Any CPU改为x86,就ok。

posted @ 2012-03-22 21:15 anakinf 阅读(16) 评论(0) 编辑

今天我登陆服务器,提示超过接数,原来上次登陆忘记注销了。刚好有急事,在网上搜索了半天,在园子里也找了,都没用,功夫不负有心人,找到了以下方法,希望对大家有用。

开始→运行→输入下面代码:

mstsc /console /v:[IP地址]:[端口]

确定,就可以连接服务器进行登陆了。

网上有黑客工具可以,也可以修改服务器的连接数,但最简单的方法莫过于此。

原文地址:http://www.xker.com/page/e2010/0126/92810.html

posted @ 2012-02-16 23:04 anakinf 阅读(86) 评论(0) 编辑

转自:http://www.cnblogs.com/lmyhao/archive/2011/01/11/1932834.html

在文章的开头,我想事先说明,本文并非诱导读者恶意反编译他人作品,盗取他人代码成果,只是为了学习Silverlight讨论而用。

在互联网上,每天都会有很多Silverlight项目发布,有的是开源,有的只是为了展示。在Silverlight学习的初期,多数都需要模仿学习,所以,发现一个Silverlight项目,能够查看其中的源代码是最好不过了。但是,Silverlight发布,只是发布资源包XAP,而所有资源都在这个XAP文件中,该如何查看其中的资源和代码呢?下面我将演示反编译的方法,演示中我将用Silverlight.Net首页Silverlight作为例子。

1. 首先打开Silverlight.Net,该页面中包含一个Silverlight导航动画;

2. 在浏览器中,点击右键,查看页面源代码;

3. 在html源代码中搜索"xap",很快就能定位出xap的位置。

我们看到<param name="Source" value="clientBin/showcasenav/ShowcaseNav.xap" />

该value就是xap在服务器端的位置。

4. 我们使用同样的域名,来获取当前的xap文件。打开新的浏览器,在地址栏输入: http://www.silverlight.net/clientBin/showcasenav/ShowcaseNav.xap

这时浏览器会提示你下载该xap文件。

5. 重新命名下载下来的文件ShowcaseNav.xap,为 ShowcaseNav.zip. 然后解压该压缩包到同一个目录. 在目录中,会看到该Silverlight使用的装配单文件,以及类库,和配置文件.

6. 下面可以使用大家都比较熟悉的反编译软件,Reflector来查看DLL中的资源和代码文件。 点击下载Reflector

7. 在Reflector中双击“MsCommunities.Silverlight.ShowcaseNav”,展开文件会看到DLL和Resources,其中Resources中包含这所有的xaml文件和图片文件,DLL中都是类库后台代码。

8. 点击进入Reources目录,双击“MsCommunities.Silverlight.ShowcaseNav.g.resources”,在Reflector右边可以看到资源列表,选中任意一个想查看代码的文件,点击右键“Save As”,保存到本地,即可使用VS2008或者文本编辑软件查看代码内容。

9. 对应着每个资源文件会有类库代码,双击“MsCommunities.Silverlight.ShowcaseNav.dll”,再双击“MsCommunities.Silverlight.ShowcaseNav”,可以查看各个类库文件,这里,我们选中"ItemAdvanceControl“,在Reflector右边即可看到反编译代码。

至此,我们已经讲述如何反编译Silverlight项目,查看其源代码,下面我想介绍一款国外的共享软件,也可以达到上面同样的效果 - Silverlight SPY,下载地址:http://silverlightspy.com/silverlightspy/download-silverlight-spy/

Silverlight SPY是一款共享软件,该软件可以自动获取XAP文件,帮你解析XAP中的文件,该软件和Reflector配合同样可以查看Silverlight项目源代码。同时,可以分解项目中的样式和UI,以及Isolated Storege展示给开发人员。我们在地址栏输入Silverlight.NET地址,即可得到,双击任何一个资源,在View窗口即可查看。

posted @ 2012-02-12 20:08 anakinf 阅读(205) 评论(4) 编辑
  1. public class UnitTransformUtil {
  2. /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */
  3. public static int dip2px(Context context, float dpValue) {
  4. final float scale = context.getResources().getDisplayMetrics().density;
  5. return (int) (dpValue * scale + 0.5f);
  6. }
  7. /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */
  8. public static int px2dip(Context context, float pxValue) {
  9. final float scale = context.getResources().getDisplayMetrics().density;
  10. return (int) (pxValue / scale + 0.5f);
  11. }
  12. }
posted @ 2012-02-01 20:24 anakinf 阅读(50) 评论(1) 编辑

Android 4.0引入了一项很重要的技术就是 WiFiDirect (WiFi直连) ,它可以让WiFi设备无需热点即可实现两个WiFi设备的P2P数据交换。使用最新的Android 4.0 SDK,最低API Level 14才支持此项技术,在SDK的例子中我们可以看到很多界面用到了Android 3.0时代的Fragment容器。

首先我们需要实现android.net.wifi.p2p.WifiP2pManager.ChannelListener 接口来获取支持WiFi直连的Android设备。

public class WiFiDirectActivity extends Activityimplements ChannelListener, DeviceActionListener {

public static final String TAG = "wifidirectdemo";
private WifiP2pManager manager;
private boolean isWifiP2pEnabled = false;
private boolean retryChannel = false;

private final IntentFilter intentFilter = new IntentFilter();
private Channel channel;
private BroadcastReceiver receiver = null;

public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) { //设置一个标记是否启用WiFi直连
this.isWifiP2pEnabled = isWifiP2pEnabled;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//注册所需要处理的action,比如WiFi连接、状态改变等。

intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null); //实例化WifiP2pManager对象
}

@Override
public void onResume() {
super.onResume();
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
registerReceiver(receiver, intentFilter);
}

@Override
public void onPause() {
super.onPause();
unregisterReceiver(receiver);
}

public void resetData() {
DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager()
.findFragmentById(R.id.frag_list);
DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager()
.findFragmentById(R.id.frag_detail);
if (fragmentList != null) {
fragmentList.clearPeers();
}
if (fragmentDetails != null) {
fragmentDetails.resetViews();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_items, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.atn_direct_enable:
if (manager != null && channel != null) {

startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
} else {
Log.e(TAG, "channel or manager is null");
}
return true;

case R.id.atn_direct_discover:
if (!isWifiP2pEnabled) {
Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning,
Toast.LENGTH_SHORT).show();
return true;
}
final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
.findFragmentById(R.id.frag_list);
fragment.onInitiateDiscovery();
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {

@Override
public void onSuccess() {
Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated",
Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(int reasonCode) {
Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode,
Toast.LENGTH_SHORT).show();
}
});
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
public void showDetails(WifiP2pDevice device) {
DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
.findFragmentById(R.id.frag_detail);
fragment.showDetails(device);

}

@Override
public void connect(WifiP2pConfig config) {
manager.connect(channel, config, new ActionListener() {

@Override
public void onSuccess() {
// WiFiDirectBroadcastReceiver will notify us. Ignore for now.
}

@Override
public void onFailure(int reason) {
Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.",
Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void disconnect() {
final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
.findFragmentById(R.id.frag_detail);
fragment.resetViews();
manager.removeGroup(channel, new ActionListener() {

@Override
public void onFailure(int reasonCode) {
Log.d(TAG, "Disconnect failed. Reason :" + reasonCode);

}

@Override
public void onSuccess() {
fragment.getView().setVisibility(View.GONE);
}

});
}

@Override
public void onChannelDisconnected() {
// we will try once more
if (manager != null && !retryChannel) {
Toast.makeText(this, "Channel lost. Trying again", Toast.LENGTH_LONG).show();
resetData();
retryChannel = true;
manager.initialize(this, getMainLooper(), this);
} else {
Toast.makeText(this,
"Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.",
Toast.LENGTH_LONG).show();
}
}

@Override
public void cancelDisconnect() {

if (manager != null) {
final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
.findFragmentById(R.id.frag_list);
if (fragment.getDevice() == null
|| fragment.getDevice().status == WifiP2pDevice.CONNECTED) {
disconnect();
} else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE
|| fragment.getDevice().status == WifiP2pDevice.INVITED) {

manager.cancelConnect(channel, new ActionListener() {

@Override
public void onSuccess() {
Toast.makeText(WiFiDirectActivity.this, "Aborting connection",
Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(int reasonCode) {
Toast.makeText(WiFiDirectActivity.this,
"Connect abort request failed. Reason Code: " + reasonCode,
Toast.LENGTH_SHORT).show();
}
});
}
}

}
}

posted @ 2012-02-01 20:23 anakinf 阅读(124) 评论(0) 编辑

Android横竖屏要解决的问题应该就两个:

一.布局问题

二.重新载入问题

1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上android:screenOrientation属性,他有以下几个参数:

"unspecified":默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.
"landscape":横屏显示(宽比高要长)
"portrait":竖屏显示(高比宽要长)
"user":用户当前首选的方向
"behind":和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
"sensor":有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。
"nosensor":忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。

也可以在Java代码中通过setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)来设置。

如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局。可以通过以下方法来切换布局:

1)在res目录下建立layout-land和layout-port目录,相应的layout文件不变,比如main.xml。layout-land是横屏的layout,layout-port是竖屏的layout,其他的不用管,模拟器会自动寻找。

2)通过this.getResources().getConfiguration().orientation来判断当前是横屏还是竖屏然后来加载相应的xml布局文件。因为当屏幕变为横屏的时候,系统会重新呼叫当前Activity的onCreate方法,你可以把以下方法放在你的onCreate中来检查当前的方向,然后可以让你的setContentView来载入不同的layout xml.

[java] view plaincopyprint?

  1. if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
  2. Log.i("info", "landscape"); // 横屏
  3. }
  4. else if (this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
  5. Log.i("info", "portrait"); // 竖屏
  6. }

if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ Log.i("info", "landscape"); // 横屏 } else if (this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) { Log.i("info", "portrait"); // 竖屏 }

在onConfigurationChanged()方法中也可以检测拥有硬键盘的键盘状态

[java] view plaincopyprint?

  1. //检测实体键盘的状态:推出或者合上
  2. if (newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_NO){
  3. //实体键盘处于推出状态,在此处添加额外的处理代码
  4. }
  5. else if (newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_YES){
  6. //实体键盘处于合上状态,在此处添加额外的处理代码
  7. }

//检测实体键盘的状态:推出或者合上 if (newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_NO){ //实体键盘处于推出状态,在此处添加额外的处理代码 } else if (newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_YES){ //实体键盘处于合上状态,在此处添加额外的处理代码 }

2.重新载入问题。如果不需要从新载入,可以在AndroidManifest.xml中加入配置android:configChanges="orientation|keyboardHidden",配置android:configChanges的作用就是如文档所说的:Specify one or more configuration changesthat the activity will handle itself. If not specified, the activity will berestarted if any of these configuration changes happen in the system。这样在程序中Activity就不会重复的调用onCreate()甚至不会调用onPause、onResume.只会调用一个onConfigurationChanged(Configuration newConfig)。如果需要重新载入,则不需要做任何修改。不过如果需要在重新载入过程中保存之前的操作内容或数据,则需要保存之前的数据。然后在activity的onCreate()中取出来。当然,如此就不能设置android:configChanges()了,否则就不会调用onCreate()方法。

如果要彻底禁止翻转,可以设置android:screenOrientation的属性为nosensor,如此就可以忽略重力感应带来的麻烦了。不过在模拟器上不管用,在真机上是正确的。android:screenOrientation="portrait"

则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。

android:screenOrientation="landscape",为横屏显示。

这里提一个小知识,Android模拟器中,快捷键"Ctrl+F11/F12"可以实现转屏。

posted @ 2012-02-01 20:22 anakinf 阅读(179) 评论(1) 编辑

保存到数据库的内容:
byte[] b=System.Text.Encoding.Default.GetBytes(this.richTextBox1.Rtf);
读取:
SqlConnection con=new SqlConnection("...");
string str="select 内容 from 数据库 where ...";//确保只返回一行
SqlCommand comm=new SqlCommand(str,con);
if (con.State==ConnectionState.Closed)
con.Open();
byte[] b=(byte[])comm.ExecuteScalar();
MemoryStream stream=new MemoryStream(b,true);
stream.Write(b,0,b.Length);
this.richTextBox1.Clear();
string s=System.Text.Encoding.Default.GetString(b,0,b.Length);
this.richTextBox1.Rtf=@s;
stream.Close();

posted @ 2012-02-01 15:33 anakinf 阅读(59) 评论(0) 编辑

如何保存RichTextBox的文本到数据库,包括格式等等,然后需要的再从数据库取出来,并且显示到RichTextBox中。

其实,RichTextBox的文本是一个FlowDocument类型的对象,我们只需要利用XamlReader和XamlWriter就能很好的完成上述工作。



【保存Document到流】

FlowDocument document = richTextBox.Document;

Stream s = new MemoryStream(); // 其他的什么Stream类型都没问题
XamlWriter.Save(document, s);

// 拿到s之后,再转化成二进制数据写到数据库就OK了

byte[] data = new byte[s.Length];

s.Position = 0;

s.Read(byte, 0, s.Length);

s.Close();

// 拿着data干啥都行

// ……



【从数据库中读取】

// data是从数据库中读出来的二进制数据

Stream s = new MemoryStream(data);

FlowDocument doc = XamlReader.Load(s) as FlowDocument;
s.Close();
richTextBox.Document = doc;



PS:有人问过我如何对RichTextBox的Document属性做绑定,由于RichTextBox的Document属性不是一个DependencyProperty,

所以我采用的是继承RichTextBox,自己定义一个BindableDocument的DependencyProperty来做。

public class BindableRichTextBox : RichTextBox
{
public FlowDocument BindableDocument
{
get { return (FlowDocument)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}

// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("BindableDocument", typeof(FlowDocument), typeof(BindableRichTextBox), new UIPropertyMetadata(null, new PropertyChangedCallback(OnTextPropertyChanged)));

private static void OnTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
BindableRichTextBox textBox = sender as BindableRichTextBox;
if (textBox != null)
{
textBox._changeFromBinding = true;
textBox.OnTextPropertyChanged(e);
}
}

// 防止死锁,比如A变了通知B,B变了又通知A
private bool _changeFromBinding = false;

// 当BindableDocument属性变化时,通知Document属性
protected virtual void OnTextPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (_changeFromBinding)
{
this.Document = e.NewValue as FlowDocument;
}
}

// 当Document属性变化时,通知BindableDocument属性
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
if (!_changeFromBinding)
{
this.BindableDocument = this.Document;
}
// 放到外面
_changeFromBinding = false;
}
}

posted @ 2012-02-01 15:21 anakinf 阅读(149) 评论(0) 编辑

最近在网上发现这篇文章,看看自己的编程习惯掌握了多少?
1. 避免将多个类放在一个文件里面。
2. 一个文件应该只有一个命名空间,避免将多个命名空间放在同一个文件里面。
3. 一个文件最好不要超过500行的代码(不包括机器产生的代码)。
4. 一个方法的代码长度最好不要超过25行。
5. 避免方法中有超过5个参数的情况。使用结构来传递多个参数。
6. 每行代码不要超过80个字符。
7. 不要手工的修改机器产生的代码。
a) 如果需要编辑机器产生的代码,编辑格式和风格要符合该编码标准。
b) Use partial classes whenever possible to factor out the maintained portions.
8. 避免利用注释解释显而易见的代码。
a) 代码应该可以自解释。好的代码由可读的变量和方法命名因此不需要注释。
9. Document only operational assumptions, algorithm insights and so on.
10. 避免使用方法级的文档。
a) 使用扩展的API文档说明之。
b) 只有在该方法需要被其他的开发者使用的时候才使用方法级的注释。(在C#中就是///)
11. 不要硬编码数字的值,总是使用构造函数设定其值。
12. 只有是自然结构才能直接使用const,比如一个星期的天数。
13. 避免在只读的变量上使用const。如果想实现只读,可以直接使用readonly。

C# code

public class MyClass { public readonly int Number; public MyClass(int someValue) { Number = someValue; } public const int DaysInWeek = 7; }


14. 每个假设必须使用Assert检查
a) 平均每15行要有一次检查(Assert)
C# code

using System.Diagnostics; object GetObject() {…} object obj = GetObject(); Debug.Assert(obj != null);


15. 代码的每一行都应该通过白盒方式的测试。
16. 只抛出已经显示处理的异常。
17. 在捕获(catch)语句的抛出异常子句中(throw),总是抛出原始异常维护原始错误的堆栈分配。
C# code

catch(Exception exception) { MessageBox.Show(exception.Message); throw ; //和throw exception一样。 }


18. 避免方法的返回值是错误代码。
19. 尽量避免定义自定义异常类。
20. 当需要定义自定义的异常时:
a) 自定义异常要继承于ApplicationException。
b) 提供自定义的序列化功能。
21. 避免在单个程序集里使用多个Main方法。
22. 只对外公布必要的操作,其他的则为internal。
23. Avoid friend assemblies, as it increases inter-assembly coupling.
24. Avoid code that relies on an assembly running from a particular location.
25. 使应用程序集尽量为最小化代码(EXE客户程序)。使用类库来替换包含的商务逻辑。
26. 避免给枚举变量提供显式的值。
//正确方法
public enum Color
{
Red,Green,Blue
}
//避免
public enum Color {
Red = 1,Green = 2,Blue = 3
}
27. 避免指定特殊类型的枚举变量。
//避免
public enum Color : long {Red,Green,Blue}
28. 即使if语句只有一句,也要将if语句的内容用大括号扩起来。
29. 避免使用trinary条件操作符。
30. 避免在条件语句中调用返回bool值的函数。可以使用局部变量并检查这些局部变量。
bool IsEverythingOK()
{…}
//避免
if (IsEverythingOK ())
{…}
//替换方案
bool ok = IsEverythingOK();
if (ok)
{…}
31. 总是使用基于0开始的数组。
32. 在循环中总是显式的初始化引用类型的数组。
C# code

public class MyClass { MyClass[] array = new MyClass[100]; for(int index = 0; index < array.Length; index++) array[index] = new MyClass(); }


33. 不要提供public 和 protected的成员变量,使用属性代替他们。
34. 避免在继承中使用new而使用override替换。
35. 在不是sealed的类中总是将public 和 protected的方法标记成virtual的。
36. 除非使用interop(COM+ 或其他的dll)代码否则不要使用不安全的代码(unsafe code)。
37. 避免显示的转换,使用as操作符进行兼容类型的转换。 Dog dog = new GermanShepherd();
GermanShepherd shepherd = dog as GermanShepherd;
if (shepherd != null )
{…}
38. 当类成员包括委托的时候
a) Copy a delegate to a local variable before publishing to avoid concurrency race condition.
b) 在调用委托之前一定要检查它是否为null
public class MySource
{
public event EventHandler MyEvent;
public void FireEvent()
{
EventHandler temp = MyEvent;
if(temp != null )
{
temp(this,EventArgs.Empty);
}
}
}
39. 不要提供公共的事件成员变量,使用事件访问器替换这些变量。
public class MySource
{
MyDelegate m_SomeEvent ;
public event MyDelegate SomeEvent
{
add
{
m_SomeEvent += value;
}
remove
{
m_SomeEvent -= value;
}
}
}
40. 使用一个事件帮助类来公布事件的定义。
41. 总是使用接口。
42. 类和接口中的方法和属性至少为2:1的比例。
43. 避免一个接口中只有一个成员。
44. 尽量使每个接口中包含3-5个成员。
45. 接口中的成员不应该超过20个。
a) 实际情况可能限制为12个
46. 避免接口成员中包含事件。
47. 避免使用抽象方法而使用接口替换。
48. 在类层次中显示接口。
49. 推荐使用显式的接口实现。
50. 从不假设一个类型兼容一个接口。Defensively query for that interface.
C# code

SomeType obj1; IMyInterface obj2; /* 假设已有代码初始化过obj1,接下来 */ obj2 = obj1 as IMyInterface; if (obj2 != null) { obj2.Method1(); } else { //处理错误 }


51. 表现给最终用户的字符串不要使用硬编码而要使用资源文件替换之。
52. 不要硬编码可能更改的基于配置的字符串,比如连接字符串。
53. 当需要构建长的字符串的时候,使用StringBuilder不要使用string
54. 避免在结构里面提供方法。
a) 建议使用参数化构造函数
b) 可以重裁操作符
55. 总是要给静态变量提供静态构造函数。
56. 能使用早期绑定就不要使用后期绑定。
57. 使用应用程序的日志和跟踪。
58. 除非在不完全的switch语句中否则不要使用goto语句。
59. 在switch语句中总是要有default子句来显示信息(Assert)。
int number = SomeMethod();
switch (number)
{
case 1:
Trace.WriteLine("Case 1:");
break;
case 2:
Trace.WriteLine("Case 2:");
break;
default:
Debug.Assert(false);
break;
}
60. 除非在构造函数中调用其他构造函数否则不要使用this指针。
// 正确使用this的例子
public class MyClass
{
public MyClass(string message )
{
}
public MyClass() : this("hello")
{
}
}
61. 除非你想重写子类中存在名称冲突的成员或者调用基类的构造函数否则不要使用base来访问基类的成员。
// 正确使用base的例子
public class Dog
{
public Dog(string name)
{}
virtual public void Bark( int howLong)
{}
}
public class GermanShepherd : Dog
{
public GermanShe pherd(string name): base (name)
{}
override public void Bark(int howLong)
{
base .Bark(howLong);
}
}
62. 基于模板的时候要实现Dispose()和Finalize()两个方法。
63. 通常情况下避免有从System.Object转换来和由System.Object转换去的代码,而使用强制转换或者as操作符替换。
class SomeClass
{}
//避免:
class MyClass <T>
{
void SomeMethod(T t)
{
object temp = t;
SomeClass obj = (SomeClass)temp;
}
}
// 正确:
class MyClass <T> where T : SomeClass
{
void SomeMethod(T t)
{
SomeClass obj = t;
}
}
64. 在一般情况下不要定影有限制符的接口。接口的限制级别通常可以用强类型来替换之。
public class Customer
{…}
//避免:
public interface IList <T> where T : Customer
{…}
//正确:
public interface ICustomerList : IList <Customer>
{…}
65. 不确定在接口内的具体方法的限制条件。
66. 总是选择使用C#内置(一般的generics)的数据结构。
posted @ 2012-01-17 10:12 anakinf 阅读(541) 评论(3) 编辑
摘要: 在Android群里,经常会有人问我,Android Log是怎么用的,今天我就把从网上以及SDK里东拼西凑过来,让大家先一睹为快,希望对大家入门Android Log有一定的帮助. android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i() Log.w() 以及 Log.e() 。根据首字母对应VERBOSE,DEBUG,INFO, WARN,ERRO...阅读全文
posted @ 2012-01-04 14:09 anakinf 阅读(173) 评论(0) 编辑