CShape&WPF笔记

问题:调试正常,编译exe后无法正常运行(界面不显示)

经过排查,确认了是数据绑定的原因,有一种情况,当绑定源的属性为 {get {…}, private set{…} } 或者 {get {…}, protect set{…} } 时。若绑定模式为TwoWay就会导致这个问题。

语法 <in T>或<out T>

定义泛型时,in表示泛型不可以改变,即使它是对象类型。

in和out用在范型interface和delegate中, 用来支持逆变和协变(in是逆变,out是协变)。协变保留赋值兼容性,逆变与之相反。

参考内容

特性 [assembly: InternalsVisibleTo(“xxx”)]

对于访问等级为internal的类型,如果使用这个特性可以指定以xxx名称的程序集有访问权限。参考内容

特性 [Export]

参考内容

微软给我们提供的强大的组件化开发框架:System.ComponentModel.Composition。我也是刚刚接触不久,下面我就班门弄斧,向大家展示下一个简单的demo。

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Reflection;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Go go = new Go();
 
            //获取当前执行的程序集中所有的标有特性标签的代码段
            AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
 
            //将所有Export特性标签存放进组件容器中(其实是一个数组里面)
            CompositionContainer container = new CompositionContainer(catalog);
 
            //找到所传入对象中所有拥有Import特性标签的属性,并在组件容器的数组中找到与这些属性匹配的Export特性标签所标注的类,然后进行实例化并给这些属性赋值。
            //简而言之,就是找到与Import对应的Export所标注的类,并用这个类的实例来给Import所标注的属性赋值,用于解耦。
            container.ComposeParts(go);
 
            if (go.test!=null)
            {
                go.test.show();
            }
            Console.ReadLine();
        }
    }
 
    //定义一个测试接口
    interface ITest {
        void show();
 
    }
 
    //Export出去的类型和名称都要和Import标注的属性匹配,类型可以写ITest, 也可以写Test
    [Export("wakaka",typeof(ITest))]
    class Test : ITest
    {
        public void show()
        {
            Console.WriteLine("OK");
        }
    }
 
    class Go
    {
        [Import("wakaka")]
        public ITest test { get; set; }
    }
}

One thought on “CShape&WPF笔记

  1. Long time reader, first time commenter — so, thought I’d drop a comment..
    — and at the same time ask for a favor.

    Your wordpress site is very simplistic – hope you don’t mind me asking what
    theme you’re using? (and don’t mind if I steal it?
    :P)

    I just launched my small businesses site –also built in wordpress like yours– but
    the theme slows (!) the site down quite a bit.

    In case you have a minute, you can find it by searching for “royal cbd” on Google (would appreciate any feedback)

    Keep up the good work– and take care of yourself during the coronavirus scare!

    ~Justin

    1. The theme use is “Specia” .

      I’m not good at website optimization, suggestion is
      1. Inspect your site source to find the slow response elements;
      2. Use some optimization plugins like Hyper Cache;
      3. Consider the server system, runtime or network optimization;

发表评论

电子邮件地址不会被公开。 必填项已用*标注

5 × 5 =