1.Reflection 與 dynamic 都是在 runtime 時可操作 object 相關 methods / properties。
2.Reflection 可在 runtime 時檢閱 object 的 meta-data,如 methods / properties,也可以在 runtime 時 invoke 物件的 methods / properties。
3.dynamic 是在 .NET 4.0 時才有的語法。宣告 dynamic 的物件,該物件會在 runtime 時才確定執行物件的 methods / properties。換句話說,在 compiler 階段該物件的 methods / properties 若不存在,是不會發生錯誤,要等到 runtime 執行時,才會有錯誤訊息。
4.dynamic 的內部也是使用 Reflection。
5.使用 Reflection 可以呼叫 public 與 private 的 methods / properties,但是 dynamic 只能呼叫 public 的 methods / properties。
6.dynamic 是 instance,所以無法呼叫 static 的 methods,若要在 runtime 時呼叫 static 的 methods,只能透過 Reflection 才行。
2014/08/27
修改 .net EXE 執行程式
先準備一個 .net 的執行檔,例如為 abc.exe,在命令列中執行 ildasm abc.exe /output:xyz.il 就會產生 MSIL 的檔案。你必須了解 MSIL 相關的語法後,做一些修改,注意的是要避免 xyz.il 檔案編碼被動到,可能會造成後續的編譯失敗。修改完存檔,使用 ilasm xyz 就可以生成 xyz.exe,執行 xyz.exe 就可以看到你修改後的效果。
2014/08/23
Hadoop 平均數範例
平均數(Mean)、均值是統計中的一個重要概念,為資料彙整後的代表值。最常使用的是算數平均數,還有其他平均數的算法,請參考 http://zh.wikipedia.org/wiki/平均值。
使用 Hadoop 來計算平均數,當成 Hadoop 一個簡單的程式範例
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.*;
import java.io.IOException;
import java.util.Iterator;
public class Average {
static class AverageMapper extends MapReduceBase implements Mapper
{
@Override
public void map(LongWritable longWritable, Text value, OutputCollector
output, Reporter reporter) throws IOException {
String lines = value.toString();
int data = Integer.parseInt(lines);
output.collect(new Text("Average"), new IntWritable(data));
}
}
static class AverageReduce extends MapReduceBase implements Reducer
{
@Override
public void reduce(Text key, Iterator values, OutputCollector
output, Reporter reporter) throws IOException {
long sum = 0;
long count = 0;
while(values.hasNext())
{
sum += values.next().get();
count ++;
}
double average = (double)sum / count;
output.collect(key, new DoubleWritable(average));
}
}
public static void main(String[] args) throws Exception{
if(args.length != 2)
{
System.err.println("Usage: Average ");
System.exit(2);
}
JobConf conf = new JobConf(Average.class);
conf.setJobName("Average");
FileInputFormat.addInputPath(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
conf.setMapperClass(AverageMapper.class);
conf.setReducerClass(AverageReduce.class);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
JobClient.runJob(conf);
}
}
使用 Hadoop 來計算平均數,當成 Hadoop 一個簡單的程式範例
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.*;
import java.io.IOException;
import java.util.Iterator;
public class Average {
static class AverageMapper extends MapReduceBase implements Mapper
{
@Override
public void map(LongWritable longWritable, Text value, OutputCollector
output, Reporter reporter) throws IOException {
String lines = value.toString();
int data = Integer.parseInt(lines);
output.collect(new Text("Average"), new IntWritable(data));
}
}
static class AverageReduce extends MapReduceBase implements Reducer
{
@Override
public void reduce(Text key, Iterator
output, Reporter reporter) throws IOException {
long sum = 0;
long count = 0;
while(values.hasNext())
{
sum += values.next().get();
count ++;
}
double average = (double)sum / count;
output.collect(key, new DoubleWritable(average));
}
}
public static void main(String[] args) throws Exception{
if(args.length != 2)
{
System.err.println("Usage: Average
System.exit(2);
}
JobConf conf = new JobConf(Average.class);
conf.setJobName("Average");
FileInputFormat.addInputPath(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
conf.setMapperClass(AverageMapper.class);
conf.setReducerClass(AverageReduce.class);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
JobClient.runJob(conf);
}
}
2014/08/22
PHP HTML Input 數量限制
遇到了奇怪的問題,HTML 中有些 Input 抓的到值,有些不行?查了 PHP 的設定才發現原來 PHP 中 Input 數量有限制,預設最大為 1000 。
請參考 http://php.net/manual/en/info.configuration.php#ini.max-input-vars
max_input_vars
integer
How many input variables may be accepted (limit is applied to $_GET, $_POST and $_COOKIE superglobal separately). Use of this directive mitigates the possibility of denial of service attacks which use hash collisions. If there are more input variables than specified by this directive, an
E_WARNING
is issued, and further input variables are truncated from the request.2014/08/17
PHP 發送中文(utf-8)信發生亂碼
這幾天有人問我,為什麼使用 PHP 程式發信, Outlook 收信的內容是亂碼,我第一個直覺就是 PHP 的檔案不是 UTF-8,他第一個時間否認,說檔案是 UTF-8,但最後我去看程式後證實我是對的,檔案不是 UTF-8,將 PHP 的檔案格式改成 UTF-8 亂碼問題就解決了。
另外說明 「UTF-8 無 BOM」和「帶 BOM 的 UTF-8」的查別是:有没有 BOM,就是檔案的開頭有没有 U+FEFF。
另外說明 「UTF-8 無 BOM」和「帶 BOM 的 UTF-8」的查別是:有没有 BOM,就是檔案的開頭有没有 U+FEFF。
訂閱:
文章 (Atom)