c# 代码常用 2018(为自己)

///

C#项目中添加DLL文件引用

首先把dll文件放到应用程序… \bin\Debug\下; 然后在解决方案中添加引用:右键鼠标–>添加引用–>浏览–>选择dll放置路径后点击“确定”

在“解决方案资源管理器”中选中项目单击右键,选择“添加引用”命令,或者在菜单栏中选择“项目”/“添加引用”命令

 

//

// path unity: "Assets/Arts/Textures/aaa/bbb/ccc.png"  
                    var a = new System.IO.DirectoryInfo(path).Name; // "Assets\\Arts\\Textures\\aaa\\bbb"
                    var b = System.IO.Path.GetDirectoryName(path); // The identifier 'b' is not in the scope
                    var c = new System.IO.DirectoryInfo(b).Name; // The identifier 'c' is not in the scope
                    var cc = new System.IO.DirectoryInfo(b); // The identifier 'cc' is not in the scope
                    var d = cc.Name; // "ccc"

public A()
{
}
public A(int A) :this()
{
}
}
当A a=new A(1);
时 会先调用无参数的构造函数。在调用有参数那个

///

C#基础 Type IsAssignableFrom 类是否实现了指定的接口

using System;

namespace ConsoleApp

{

    public interface ISayHello

    {

        void SayHello();

    }

    class CapablePerson : ISayHello

    {

        public void SayHello()

        {

            throw new NotImplementedException();

        }

    }

    class UnachievablePerson

    {

    }

    class Program

    {

        static void Main(string[] args)

        {

            Type tpISayHello = typeof(ISayHello);

            Type tpCapablePersonClass = typeof(CapablePerson);

            Type tpUnachievablePersonClass = typeof(UnachievablePerson);

            if (tpISayHello.IsAssignableFrom(tpCapablePersonClass))

            {

                Console.WriteLine(tpCapablePersonClass.Name + "实现了接口" + tpISayHello.Name);

            }

            else

            {

                Console.WriteLine(tpCapablePersonClass.Name + "没有实现接口" + tpISayHello.Name);

            }

            if (tpISayHello.IsAssignableFrom(tpUnachievablePersonClass))

            {

                Console.WriteLine(tpUnachievablePersonClass.Name + "实现了接口" + tpISayHello.Name);

            }

            else

            {

                Console.WriteLine(tpUnachievablePersonClass.Name + "没有实现了接口" + tpISayHello.Name);

            }

            Console.ReadKey();

        }

    }

}

Result:

CapablePerson实现了接口ISayHello
UnachievablePerson没有实现了接口ISayHello

C# IsAssignableFrom与IsSubClassOf 判断匿名类是否继承父类

public class Dog : Animal

    {

        public string name { get; set; }

    }

    public class Animal

    {

        public string id { get; set; }

    }

    public class Main

    {

        public void Main()

        {

            Dog aa = new Dog { name = "狗", id = "动物" };

            CheckClass(aa);

        }

        public void CheckClass<T>(T entity)

        {

            bool re1 = typeof(Animal).IsAssignableFrom(typeof(T));

            //返回true的条件是Dog类直接或间接的实现了Animal类;

            bool re2 = typeof(T).IsSubclassOf(typeof(Animal));

            //返回true的条件是Dog类是Animal的子类

           

            var id = (entity as Animal).id;

        }

    }

///

首先说下 IsGenericType

用3个实例说明:

   typeof(DateTime).IsGenericType : false

   typeof(List<int>).IsGenericType: true

   typeof(Dictionary<,>).IsGenericType:true

类型如果是泛型则为 true

但是要注意以下情况:

T[], List[] 等等数组时, IsGenericType为False

typeof(T[]).GetElementType().IsGenericType才是True

接着说 IsGenericTypeDefinition

用2个实例说明:

   typeof(List<int>).IsGenericTypeDefintion : false

   typeof(List<>).IsGenericTypeDefinition :true

IsGenericTypeDefinition : 获取一个值,该值指示当前 Type 是否表示可以用来构造其他泛型类型的泛型类型定义。

也就是说表明 这个 type 是否可以用于构建泛型定义

比如 List<> 可以通过反射构建出 List,List

例子:

var typeList = typeof(List<>);       

Type typeDataList = typeList.MakeGenericType( typeof(DateTime)); //通过List<>构建出List<DateTime>

最后说下 IsGenericParameter

这个Property用于表明当前类型是一个T类型

例如:

    typeof(List<>).GetGenericArguments()

返回: new Type[]{ typeof(T) }

此时:

typeof(List<>).GetGenericArguments()

返回: new Type[]{ typeof(T) }

此时:

        typeof(T).IsGenericParameter == True

        typeof(T).GenericParameterPosition  == 0   

对比:

typeof(List<DateTime>).GetGenericArguments()

返回: new Type[]{ typeof(DateTime) }

此时:

    typeof(DateTime).IsGenericParameter  == False

    typeof(DateTime).GenericParameterPosition    : throw exception

///

Dot Net 高性能代码/

C# AggressiveInlining: MethodImpl Attribute
Benchmark and test the AggressiveInlining option. Use MethodImplOptions.AggressiveInlining.
AggressiveInlining. The JIT compiler logically determines which methods to inline. But sometimes we know better than it does. With AggressiveInlining, we give the compiler a hint. We tell it that the method should be inlined.
Attribute
Optimization

Example. This example benchmarks a method with no attribute, and with AggressiveInlining. The method body contains several lines of useless code. This makes the method large in bytes, so the JIT compiler may decide not to inline it.
Benchmark
And: We apply the MethodImplOptions.AggressiveInlining option to Method2. This is an enum.

Result: We see that with no options, the method calls required seven nanoseconds each.

But: With inlining specified (with AggressiveInlining), the calls required less than one nanosecond each.

C# program that uses AggressiveInlining

using System;

using System.Diagnostics;

using System.Runtime.CompilerServices;

class Program {

const int _max = 10000000;

static void Main() {

 // ... Compile the methods.

 Method1();

Method2();

int sum = 0;

var s1 = Stopwatch.StartNew();

for (int i = 0; i < _max; i++) {

sum += Method1(); }

s1.Stop();

var s2 = Stopwatch.StartNew();

for (int i = 0; i < _max; i++) {

sum += Method2(); }

s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));

 Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); }

static int Method1() {

// ... No inlining suggestion.

return "one".Length + "two".Length + "three".Length + "four".Length + "five".Length + "six".Length + "seven".Length + "eight".Length + "nine".Length + "ten".Length;

}

[MethodImpl(MethodImplOptions.AggressiveInlining)]

static int Method2() {

// ... Aggressive inlining.

return "one".Length + "two".Length + "three".Length + "four".Length + "five".Length + "six".Length + "seven".Length + "eight".Length + "nine".Length + "ten".Length;

 }

}

Output

7.34 ns No options

0.32 ns MethodImplOptions.AggressiveInlining

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值