LinQtoEntity类型转换

使用EntityFrameWork时,经常会用到lambda表达式来做查询等操作,由于EF要根据表达式生成最终操作数据库的SQL,所以在表达式中加入其它方法如”parse”,”convert”可能会导致不被LinqToEntity识别,异常如下:

System.NotSupportedException: LINQ to Entities does not recognize the method int.Parse(System.String)

但在实际项目中往往会遇到实体字段类型与参数类型需要转换并比较的问题:

问题1: 字段int型与参数string型的比较

例:

阅读更多

C#调用C DLL参数问题

首先回顾一下API和C#参数类型的基本对应关系:

需要注意的是这个对应关系是传入参数的对应关系,如果是输出参数,LPSTR和LPCSTR(也就是C/C++里面的char*),对应的就要是StringBuider。

例如:int TEST(char* result)

其中result是输出参数,那么在c#中,就应该使用 int TEST(StringBuider result),而不是ref string,否则会报内存错误。

阅读更多

常用C#设计模式(下)

【代理模式】

描述:给某一个对象提供一个代理,并由代理对象控制对原对象的引用。

在一些情况下,一个客户不想或者不能够直接引用一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。举几个例子,用户提供食材和菜名,“不想”自己做菜,给厨师,厨师就是一个代理。国内用户访问facebook会被墙,代理可以绕开GFW并将请求结果返回。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public interface IRequest
{
// Methods
string Request(string sourceIp,string targetIp);
}

class GFW : IRequest
{
public string Request(string sourceIp, string targetIp)
{
if (sourceIp=="国内ip" && targetIp == "敏感ip")
{
return "404错误";
}
else
{
return "请求成功";
}
}
}

class Proxy : IRequest
{
GFW gfw = new GFW();

public string Request(string sourceIp, string targetIp)
{
sourceIp = "代理ip";
return gfw.Request(sourceIp, targetIp);
}
}

//调用
GFW gfw = new GFW();
Console.Write(gfw.Request("国内ip", "敏感ip"));

//Proxy p = new Proxy();
//Console.Write(p.Request("国内ip", "敏感ip"));
阅读更多

常用C#设计模式(中)

【装饰模式】

描述:动态地给一个对象添加一些额外的职责。

有些时候我们需要给某个对象增加一些新的特性,使用继承机制是一种有效的方法,但是不够灵活。因为如果我们要使用继承扩展一个类,就必须要在编译的时候定义这个扩展的类,这是所谓的静态,也是强类型语言的特点。这时候可以使用装饰模式。

例子:

Weapon类

阅读更多

常用C#设计模式(上)

DP是coding中经常谈到的问题,虽然DP种类繁多,但是常用的DP不过几种,这里将浅析C#常用的设计模式。

【单例模式】

描述:最“简单”的设计模式,顾名思义,这个类型只有一个实例,不能创建其他实例。这个类型提供一个公共的访问点让用户操作这个实例。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Single
{
public void Print()
{
System.Console.Write("I am a singleton.");
}

private Single()
{

}

private static Single mySingle = new Single();

public static Single MySingle
{
get { return mySingle = new Single(); }
}

}
阅读更多

125 Basic C# Interview Questions and Answers

英文原文:http://theprofessionalspoint.blogspot.jp/2013/10/125-basic-c-interview-questions-and.html

Below is the list of 125 basic C# interview questions with their answers. These C# interview questions and?answers?are very simple and straight-forward which cover the basic concepts of C# mostly related to object oriented concepts. So if you are preparing for C# interview, I will suggest you to must go through these 125 C# basic interview questions and answers to revise your C# concepts. Here goes the list of 125 basic C# interview questions and answers.

1. What is C#?

C# (pronounced “C sharp”) is a simple, modern, object-oriented, and type-safe programming language. It will immediately be familiar?to C and C++ programmers. C# combines the high productivity of Rapid Application Development (RAD) languages.

2. What are the types of comment in C#?

阅读更多

wpf失去焦点事件中重获焦点

WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法
在WPF的TextBox的LostFocus事件中直接使用Focus()方法会出现死循环的问题
正确的使用方式有两种方法:
方法一:

1
2
3
4
5
6
7
8
9
10
11
private void textBox3_LostFocus(object sender, RoutedEventArgs e)
{
if (textBox3.Text != "abc")
{
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
new Action(() =>
{
textBox3.Focus();
}));
}
}

方法二,使用LostKeyboardFocus方法:

1
2
3
4
5
6
7
private void textBox3_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (textBox3.Text != "abc")
{
textBox3.Focus();
}
}

说明:在msdn上就查找到:

阅读更多

CreateBitmapSourceFromHBitmap内存泄露

用c#做图像处理的时候需要用到System.Drawing.Bitmap。在WPF中显示图像的Image控件接受的数据源是ImageSource,因此使用System.Drawing.Bitmap进行图像处理之后要把System.Drawing.Bitmap转换成ImageSource,转换方法如下:

1
2
3
4
5
6
7
System.Drawing.Bitmap m_Bitmap = new System.Drawing.Bitmap("c:temptest.jpg", false); 
IntPtr ip = m_Bitmap.GetHbitmap();
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ip, IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ip);
imageLarge.Source = bitmapSource;

其中DeleteObject的声明如下:

1
2
[DllImport("gdi32")]  
static extern int DeleteObject(IntPtr o);

使用过System.Drawing.Bitmap后一定要用DeleteObject释放掉对象,不然内存不释放,很快系统内存就消耗光了。

阅读更多

C#调用C DLL时 关于参数为char *类型的转换

如果DLL接口的输入参数为char**,也就是字符数组的数组(即字符串数组),此时在C#声明中不能直接传递string[],传递的应该是通过Encoding类对这个string[]进行编码后得到的一个char[]。

如果DLL接口的输出参数为char**,也就是字符数组的数组(即字符串数组),此时在C#声明中应该使用byte[]来做参数。然后通过Encoding类对这个byte[]进行解码,得到字符串。如下例所示:

C++ DLL接口:

1
2
3
4
5
6
7
long _stdcall USE_GetAgentGroupInfoEx(
/* [in] */ char** AgentID,
/* [in] */ long ArraySize,
/* [out] */ char** AgentName,
/* [out] */ long AgentStatus[],
/* [out] */ char** AgentDN,
/* [out] */ char** AgentIP);

C#中的声明

阅读更多

WPF将控件转化为图像

在项目中需要将datagrid这个控件转化成图像输出,思考了几种办法:

1.可以对屏幕截取,然后再根据控件相对于屏幕的坐标再次截取,即可取得控件截图。这种方法限制性很大,如图像的分辨率被限死,同时,如果控件不在屏幕中显示的话那么就无法进行截图。

2.查看datagrid源码,重新绘制图形。这个办法只要掌握,其实一劳永逸。只不过需要时间过长,略显复杂,项目中需要一种简洁的方法来操作。

3.有自带方法绘制控件。经过查阅资料,整理代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// <summary>
/// 可以对WPF中的控件抓取为图片形式.
/// </summary>
/// <param name="p_FrameworkElement">控件对象</param>
/// <param name="p_FileName">生成图片的路径</param>
private void SaveToImage(FrameworkElement p_FrameworkElement, string p_FileName)
{
System.IO.FileStream fs = new System.IO.FileStream(p_FileName, System.IO.FileMode.Create);
RenderTargetBitmap bmp = new RenderTargetBitmap((int)p_FrameworkElement.ActualWidth, (int)p_FrameworkElement.ActualHeight, 96.0, 96.0, PixelFormats.Pbgra32);
bmp.Render(p_FrameworkElement);
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(fs);
fs.Close();
fs.Dispose();
}
阅读更多