4. The solution set of the equation $x^{2}+x-1=x \mathrm{e}^{x^{2}-1}+\left(x^{2}-1\right) \mathrm{e}^{x}$ is $\mathrm{A}$ (where $\mathrm{e}$ is an irrational number, $\mathrm{e}=2.71828 \cdots, x$ is a real number), then the sum of the squares of all elements in $\mathrm{A}$ is ( ).
A. 0
B. 1
C. 2
D. 4
4. C

Let $y=x^{2}-1$, the original equation becomes $x+y=x \mathrm{e}^{y}+y \mathrm{e}^{\prime}$, that is,
$$
x\left(\mathrm{e}^{y}-1\right)+y\left(\mathrm{e}^{x}-1\right)=0
$$

Since when $x>0$, $\mathrm{e}^{x}-1>0$, thus $x$ and $\mathrm{e}^{x}-1$ have the same sign, similarly $y$ and $\mathrm{e}^{y}-1$ have the same sign, hence $x\left(\mathrm{e}^{x}-1\right) \cdot y\left(\mathrm{e}^{y}-1\right)>0 \Leftrightarrow x^{2} y^{2}>0$.
Therefore, $x\left(\mathrm{e}^{y}-1\right)+y\left(\mathrm{e}^{x}-1\right)=0 \Leftrightarrow\left\{\begin{array}{l}x\left(\mathrm{e}^{y}-1\right)=0 \\ y\left(\mathrm{e}^{x}-1\right)=0\end{array}\right.$
$\Leftrightarrow x=0$ or $y=0$ Thus, all roots of the original equation are $-1,0,1$, i.e., $A=\{-1,0,1\}$, and the sum of the squares of its elements is 2, choose $\mathrm{C}$.